/*jshint -W030 */

var expect = require('chai').expect;
var docker = require('./spec_helper').docker;
var MemoryStream = require('memorystream');

describe("#networks", function() {

  var testContainer;
  var testNetwork;

  before(function(done) {
    this.timeout(30000);
    docker.createContainer({
      Image: 'ubuntu',
      AttachStdin: false,
      AttachStdout: true,
      AttachStderr: true,
      Tty: true,
      Cmd: ['/bin/bash', '-c', 'tail -f /etc/resolv.conf'],
      OpenStdin: false,
      StdinOnce: false
    }, function(err, container) {
      if (err) return done(err);
      testContainer = container.id;
      container.start(function(err, result) {
        if (err) return done(err);

        docker.createNetwork({
          "Name": "isolated_nw",
          "Driver": "bridge",
          "IPAM": {
            "Config": [{
              "Subnet": "172.20.0.0/16",
              "IPRange": "172.20.10.0/24",
              "Gateway": "172.20.10.12"
            }]
          }
        }, function(err, network) {
          if (err) return done(err);
          testNetwork = network;
          done();
        });
      });
    });
  });

  after(function(done) {
    this.timeout(15000);
    var container = docker.getContainer(testContainer);
    container.kill(function(err, result) {
      if (err) return done(err);
      container.remove(function(err, result) {
        if (err) return done(err);

        testNetwork.remove(function(err, result) {
          if (err) return done(err);
          done();
        });
      });
    });
  });

  describe("#connect", function() {
    it("should connect a container to a network", function(done) {
      this.timeout(10000);
      var network = testNetwork;
      var container = testContainer;

      function handler(err, data) {
        expect(err).to.be.null;
        done();
      }

      network.connect({
        Container: container
      }, handler);
    });
  });

  describe("#inspect", function() {
    it("should inspect a network", function(done) {
      var network = testNetwork;

      function handler(err, data) {
        expect(err).to.be.null;
        expect(data).to.be.ok;
        expect(data.Containers).to.be.ok;
        done();
      }

      network.inspect(handler);
    });

    it("should inspect a network verbosely when verbose: true", function(done) {
      var network = testNetwork;

      function handler(err, data) {
        expect(err).to.be.null;
        expect(data).to.be.ok;
        expect(data.Containers).to.be.ok;
        done();
      }

      network.inspect({ verbose: true }, handler);
    });
  });

  describe("#disconnect", function() {
    it("should disconnect a container to a network", function(done) {
      var network = testNetwork;
      var container = testContainer;

      function handler(err, data) {
        expect(err).to.be.null;
        done();
      }

      network.disconnect({
        Container: container
      }, handler);
    });
  });

});
