Esempio n. 1
0
func (s *S) SetUpTest(c *check.C) {
	iaas.ResetAll()
	repositorytest.Reset()
	queue.ResetQueue()
	s.p = &dockerProvisioner{storage: &cluster.MapStorage{}}
	err := s.p.Initialize()
	c.Assert(err, check.IsNil)
	queue.ResetQueue()
	app.Provisioner = s.p
	s.server, err = dtesting.NewServer("127.0.0.1:0", nil, nil)
	c.Assert(err, check.IsNil)
	s.p.cluster, err = cluster.New(nil, s.p.storage,
		cluster.Node{Address: s.server.URL(), Metadata: map[string]string{"pool": "test-fallback"}},
	)
	c.Assert(err, check.IsNil)
	mainDockerProvisioner = s.p
	coll := s.p.collection()
	defer coll.Close()
	err = dbtest.ClearAllCollectionsExcept(coll.Database, []string{"users", "tokens", "teams"})
	c.Assert(err, check.IsNil)
	err = clearClusterStorage(s.clusterSess)
	c.Assert(err, check.IsNil)
	routertest.FakeRouter.Reset()
	opts := provision.AddPoolOptions{Name: "test-fallback"}
	err = provision.AddPool(opts)
	c.Assert(err, check.IsNil)
}
Esempio n. 2
0
func (s *S) SetUpTest(c *check.C) {
	config.Set("docker:api-timeout", 2)
	iaas.ResetAll()
	repositorytest.Reset()
	queue.ResetQueue()
	repository.Manager().CreateUser(s.user.Email)
	s.p = &dockerProvisioner{storage: &cluster.MapStorage{}}
	err := s.p.Initialize()
	c.Assert(err, check.IsNil)
	queue.ResetQueue()
	s.server, err = dtesting.NewServer("127.0.0.1:0", nil, nil)
	c.Assert(err, check.IsNil)
	s.p.cluster, err = cluster.New(nil, s.p.storage,
		cluster.Node{Address: s.server.URL(), Metadata: map[string]string{"pool": "test-default"}},
	)
	c.Assert(err, check.IsNil)
	mainDockerProvisioner = s.p
	err = dbtest.ClearAllCollectionsExcept(s.storage.Apps().Database, []string{"users", "tokens", "teams"})
	c.Assert(err, check.IsNil)
	err = clearClusterStorage(s.clusterSess)
	c.Assert(err, check.IsNil)
	routertest.FakeRouter.Reset()
	opts := provision.AddPoolOptions{Name: "test-default", Default: true}
	err = provision.AddPool(opts)
	c.Assert(err, check.IsNil)
	s.storage.Tokens().Remove(bson.M{"appname": bson.M{"$ne": ""}})
	s.logBuf = safe.NewBuffer(nil)
	log.SetLogger(log.NewWriterLogger(s.logBuf, true))
	s.token = createTokenForUser(s.user, "*", string(permission.CtxGlobal), "", c)
}
Esempio n. 3
0
func (s *S) SetUpTest(c *check.C) {
	queue.ResetQueue()
	err := rebuild.RegisterTask(func(appName string) (rebuild.RebuildApp, error) {
		a, err := app.GetByName(appName)
		if err == app.ErrAppNotFound {
			return nil, nil
		}
		return a, err
	})
	c.Assert(err, check.IsNil)
	routertest.FakeRouter.Reset()
	provisiontest.ProvisionerInstance.Reset()
	err = dbtest.ClearAllCollections(s.conn.Apps().Database)
	c.Assert(err, check.IsNil)
	s.user = &auth.User{Email: "*****@*****.**", Password: "******", Quota: quota.Unlimited}
	nativeScheme := auth.ManagedScheme(native.NativeScheme{})
	app.AuthScheme = nativeScheme
	_, err = nativeScheme.Create(s.user)
	c.Assert(err, check.IsNil)
	s.team = &auth.Team{Name: "admin"}
	c.Assert(err, check.IsNil)
	err = s.conn.Teams().Insert(s.team)
	c.Assert(err, check.IsNil)
	err = provision.AddPool(provision.AddPoolOptions{
		Name:        "p1",
		Default:     true,
		Provisioner: "fake",
	})
	c.Assert(err, check.IsNil)
}
Esempio n. 4
0
func (s *S) SetUpTest(c *check.C) {
	var err error
	s.srv, err = ec2test.NewServer()
	c.Assert(err, check.IsNil)
	config.Set("iaas:ec2:key-id", "mykey")
	config.Set("iaas:ec2:secret-key", "mysecret")
	config.Set("queue:mongo-database", "queue_ec2_iaas")
	queue.ResetQueue()
}
Esempio n. 5
0
func (s *S) TestCreateMachineTimeoutError(c *check.C) {
	config.Set("iaas:ec2:wait-timeout", "1")
	defer config.Unset("iaas:ec2:wait-timeout")
	var calledActions []string
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		action := r.FormValue("Action")
		calledActions = append(calledActions, action)
		if action == "DescribeInstances" {
			w.Write([]byte(`
<DescribeInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2015-10-01/">
<requestId>xxx</requestId>
<reservationSet>
      <item>
        <reservationId>r-1</reservationId>
        <instancesSet>
          <item>
            <instanceId>i-1</instanceId>
          </item>
        </instancesSet>
      </item>
</reservationSet>
</DescribeInstancesResponse>`))
			return
		}
		buf := bytes.NewBufferString(r.Form.Encode())
		req, err := http.NewRequest(r.Method, s.srv.URL()+r.RequestURI, buf)
		c.Assert(err, check.IsNil)
		for name, values := range r.Header {
			for _, value := range values {
				req.Header.Add(name, value)
			}
		}
		rsp, err := http.DefaultClient.Do(req)
		c.Assert(err, check.IsNil)
		w.WriteHeader(rsp.StatusCode)
		bytes, err := ioutil.ReadAll(rsp.Body)
		if action == "RunInstances" {
			re := regexp.MustCompile(`<dnsName>.+</dnsName>`)
			bytes = re.ReplaceAll(bytes, []byte{})
		}
		c.Assert(err, check.IsNil)
		w.Write(bytes)
	}))
	params := map[string]string{
		"endpoint": server.URL,
		"image":    "ami-xxxxxx",
		"type":     "m1.micro",
	}
	ec2iaas := newEC2IaaS("ec2")
	err := (ec2iaas.(*EC2IaaS)).Initialize()
	c.Assert(err, check.IsNil)
	_, err = ec2iaas.CreateMachine(params)
	c.Assert(err, check.ErrorMatches, `ec2: time out after .+? waiting for instance .+? to start`)
	queue.ResetQueue()
	c.Assert(calledActions[len(calledActions)-1], check.Equals, "TerminateInstances")
}
Esempio n. 6
0
func (s *HandlersSuite) SetUpTest(c *check.C) {
	queue.ResetQueue()
	err := clearClusterStorage(s.clusterSess)
	c.Assert(err, check.IsNil)
	mainDockerProvisioner = &dockerProvisioner{}
	err = mainDockerProvisioner.Initialize()
	c.Assert(err, check.IsNil)
	coll := mainDockerProvisioner.collection()
	defer coll.Close()
	err = dbtest.ClearAllCollectionsExcept(coll.Database, []string{"users", "tokens", "teams"})
	c.Assert(err, check.IsNil)
}
Esempio n. 7
0
func (s *S) SetUpTest(c *check.C) {
	config.Set("docker:api-timeout", 2)
	conn, err := db.Conn()
	c.Assert(err, check.IsNil)
	defer conn.Close()
	dbtest.ClearAllCollections(conn.Apps().Database)
	queue.ResetQueue()
	iaas.ResetAll()
	machines, _ := iaas.ListMachines()
	for _, m := range machines {
		m.Destroy()
	}
	os.Setenv("TSURU_TARGET", "http://localhost")
}
Esempio n. 8
0
func (s *HandlersSuite) SetUpTest(c *check.C) {
	config.Set("docker:api-timeout", 2)
	queue.ResetQueue()
	err := clearClusterStorage(s.clusterSess)
	c.Assert(err, check.IsNil)
	mainDockerProvisioner = &dockerProvisioner{}
	err = mainDockerProvisioner.Initialize()
	c.Assert(err, check.IsNil)
	coll := mainDockerProvisioner.Collection()
	defer coll.Close()
	err = dbtest.ClearAllCollectionsExcept(coll.Database, []string{"users", "teams"})
	c.Assert(err, check.IsNil)
	s.token = createToken(c)
	s.user, err = s.token.User()
	c.Assert(err, check.IsNil)
}
Esempio n. 9
0
func (s *HandlersSuite) startFakeDockerNode(c *check.C) (*testing.DockerServer, func()) {
	pong := make(chan struct{})
	server, err := testing.NewServer("127.0.0.1:0", nil, func(r *http.Request) {
		if strings.Contains(r.URL.Path, "ping") {
			close(pong)
		}
	})
	c.Assert(err, check.IsNil)
	url, err := url.Parse(server.URL())
	c.Assert(err, check.IsNil)
	_, portString, _ := net.SplitHostPort(url.Host)
	port, _ := strconv.Atoi(portString)
	config.Set("iaas:node-port", port)
	return server, func() {
		<-pong
		queue.ResetQueue()
	}
}
Esempio n. 10
0
func (s *S) SetUpTest(c *check.C) {
	// Reset fake routers twice, first time will remove registered failures and
	// allow pending enqueued tasks to run, second time (after queue is reset)
	// will remove any routes added by executed queue tasks.
	routertest.FakeRouter.Reset()
	routertest.HCRouter.Reset()
	queue.ResetQueue()
	routertest.FakeRouter.Reset()
	routertest.HCRouter.Reset()
	err := rebuild.RegisterTask(func(appName string) (rebuild.RebuildApp, error) {
		a, err := GetByName(appName)
		if err == ErrAppNotFound {
			return nil, nil
		}
		return a, err
	})
	c.Assert(err, check.IsNil)
	config.Set("docker:router", "fake")
	s.provisioner.Reset()
	repositorytest.Reset()
	dbtest.ClearAllCollections(s.conn.Apps().Database)
	s.createUserAndTeam(c)
	platform := Platform{Name: "python"}
	s.conn.Platforms().Insert(platform)
	s.defaultPlan = Plan{
		Name:     "default-plan",
		Memory:   1024,
		Swap:     1024,
		CpuShare: 100,
		Default:  true,
		Router:   "fake",
	}
	err = s.conn.Plans().Insert(s.defaultPlan)
	c.Assert(err, check.IsNil)
	s.Pool = "pool1"
	opts := provision.AddPoolOptions{Name: s.Pool, Default: true}
	err = provision.AddPool(opts)
	c.Assert(err, check.IsNil)
	repository.Manager().CreateUser(s.user.Email)
	factory, err := queue.Factory()
	c.Assert(err, check.IsNil)
	factory.Reset()
}
Esempio n. 11
0
func (s *S) SetUpTest(c *check.C) {
	var err error
	s.srv, err = ec2test.NewServer()
	c.Assert(err, check.IsNil)
	s.srv.SetAvailabilityZones([]ec2amz.AvailabilityZoneInfo{
		{
			AvailabilityZone: ec2amz.AvailabilityZone{
				Name:   "us-east-1b",
				Region: "us-east-1",
			},
		},
	})
	s.srv.SetInitialAttributes(map[string][]string{"default-vpc": {"vpc-123"}})
	config.Set("iaas:ec2:key-id", "mykey")
	config.Set("iaas:ec2:secret-key", "mysecret")
	config.Set("queue:mongo-url", "127.0.0.1:27017")
	config.Set("queue:mongo-database", "queue_ec2_iaas")
	queue.ResetQueue()
}
Esempio n. 12
0
func (s *HandlersSuite) TestFixContainerHandler(c *check.C) {
	queue.ResetQueue()
	cleanup, server, p := startDocker("9999")
	defer cleanup()
	coll := p.collection()
	defer coll.Close()
	conn, err := db.Conn()
	c.Assert(err, check.IsNil)
	defer conn.Close()
	err = conn.Apps().Insert(&app.App{Name: "makea"})
	c.Assert(err, check.IsNil)
	defer conn.Apps().RemoveAll(bson.M{"name": "makea"})
	err = coll.Insert(
		container{
			ID:       "9930c24f1c4x",
			AppName:  "makea",
			Type:     "python",
			Status:   provision.StatusStarted.String(),
			IP:       "127.0.0.4",
			HostPort: "9025",
			HostAddr: "127.0.0.1",
		},
	)
	c.Assert(err, check.IsNil)
	defer coll.RemoveAll(bson.M{"appname": "makea"})
	var storage cluster.MapStorage
	storage.StoreContainer("9930c24f1c4x", server.URL)
	mainDockerProvisioner = p
	mainDockerProvisioner.cluster, err = cluster.New(nil, &storage,
		cluster.Node{Address: server.URL},
	)
	c.Assert(err, check.IsNil)
	request, err := http.NewRequest("POST", "/fix-containers", nil)
	c.Assert(err, check.IsNil)
	recorder := httptest.NewRecorder()
	err = fixContainersHandler(recorder, request, nil)
	c.Assert(err, check.IsNil)
	cont, err := p.getContainer("9930c24f1c4x")
	c.Assert(err, check.IsNil)
	c.Assert(cont.IP, check.Equals, "127.0.0.9")
	c.Assert(cont.HostPort, check.Equals, "9999")
}
Esempio n. 13
0
func (s *cloudstackSuite) TestCreateMachineTimeout(c *check.C) {
	done := make(chan bool)
	var calls []string
	fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		cmd := r.URL.Query().Get("command")
		calls = append(calls, cmd)
		w.Header().Set("Content-type", "application/json")
		if cmd == "queryAsyncJobResult" {
			status := jobInProgress
			if len(calls) > 4 {
				status = 1
			}
			fmt.Fprintf(w, `{"queryasyncjobresultresponse": {"jobstatus": %d}}`, status)
		}
		if cmd == "deployVirtualMachine" {
			fmt.Fprintln(w, `{"deployvirtualmachineresponse": {"id": "0366ae09-0a77-4e2b-8595-3b749764a107", "jobid": "test"}}`)
		}
		if cmd == "listVirtualMachines" {
			json := `{"listvirtualmachinesresponse":{"count":1,"virtualmachine":[{"id":"0366ae09-0a77-4e2b-8595-3b749764a107","name":"vm-0366ae09-0a77-4e2b-8595-3b749764a107","projectid":"a98738c9-5acd-43e3-b1a1-972a3db5b196","nic":[{"id":"40cd6225-9475-44a3-8288-d7a9a485d8ac","networkid":"18c20437-df18-4757-8435-1230248f955b","ipaddress":"10.24.16.241"}],"jobid":"82a574cc-43f2-440d-8774-e638065c37af"}]}}`
			fmt.Fprintln(w, json)
		}
		if cmd == "listVolumes" {
			fmt.Fprintln(w, `{"listvolumesresponse": {"volume": [ {"id": "v1", "type": "ROOT"}, {"id": "v2", "type": "DATADISK"} ]}}`)
		}
		if cmd == "destroyVirtualMachine" {
			fmt.Fprintln(w, `{"destroyvirtualmachineresponse": {"jobid": "job1"}}`)
		}
		if cmd == "detachVolume" {
			fmt.Fprintln(w, `{"detachvolumeresponse": {"jobid": "job1"}}`)
		}
		if cmd == "deleteVolume" {
			done <- true
		}
	}))
	defer fakeServer.Close()
	config.Set("iaas:cloudstack:url", fakeServer.URL)
	config.Set("iaas:cloudstack:wait-timeout", "1")
	defer config.Unset("iaas:cloudstack:wait-timeout")
	cs := newCloudstackIaaS("cloudstack")
	err := (cs.(*CloudstackIaaS)).Initialize()
	c.Assert(err, check.IsNil)
	params := map[string]string{
		"projectid":         "val",
		"networkids":        "val",
		"templateid":        "val",
		"serviceofferingid": "val",
		"zoneid":            "val",
	}
	_, err = cs.CreateMachine(params)
	c.Assert(err, check.ErrorMatches, `cloudstack: time out after .+? waiting for instance .+ to start`)
	select {
	case <-done:
	case <-time.After(time.Minute):
		c.Fatal("timeout waiting for api calls")
	}
	queue.ResetQueue()
	c.Assert(calls, check.DeepEquals, []string{
		"deployVirtualMachine",
		"queryAsyncJobResult",
		"queryAsyncJobResult",
		"queryAsyncJobResult",
		"queryAsyncJobResult",
		"listVirtualMachines",
		"listVolumes",
		"destroyVirtualMachine",
		"queryAsyncJobResult",
		"detachVolume",
		"queryAsyncJobResult",
		"deleteVolume",
	})
}
Esempio n. 14
0
func (s *cloudstackSuite) SetUpTest(c *check.C) {
	config.Set("queue:mongo-url", "127.0.0.1:27017")
	config.Set("queue:mongo-database", "queue_cloudstack_iaas")
	queue.ResetQueue()
}