Beispiel #1
0
func TestActorClient(t *testing.T) {
	config.Config.UseAuth = true
	indexer.Initialize(config.Config)
	c, _ := client.New("fooclient")
	gob.Register(c)
	c.Save()
	c1, err := GetReqUser("fooclient")
	if err != nil {
		t.Errorf(err.Error())
	}
	y := c1.IsSelf(c)
	if y == false {
		t.Errorf("self not equal to self")
	}
	c2, _ := client.New("foo2client")
	y = c1.IsSelf(c2)
	if y != false {
		t.Errorf("client %s was equal to client %s, but should not have been", c1.GetName(), c2.Name)
	}

	u, _ := user.New("foouser")
	gob.Register(u)
	u.Save()

	y = c1.IsSelf(u)
	if y != false {
		t.Errorf("client %s was equal to user %s, but should not have been", c1.GetName(), u.Username)
	}

	c.Delete()
	c2.Delete()
	u.Delete()
}
Beispiel #2
0
func TestActorUser(t *testing.T) {
	config.Config.UseAuth = true
	u, err := user.New("foo1user")
	if err != nil {
		t.Errorf(err.Error())
	}
	u.Save()
	u1, err := GetReqUser("foo1user")
	if err != nil {
		t.Errorf(err.Error())
	}
	y := u1.IsSelf(u)
	if y == false {
		t.Errorf("self not equal to self")
	}
	u2, _ := user.New("foo2user")
	y = u1.IsSelf(u2)
	if y != false {
		t.Errorf("user %s was equal to user %s, but should not have been", u1.GetName(), u2.Username)
	}

	c, _ := client.New("foo1client")
	c.Save()

	y = u1.IsSelf(c)
	if y != false {
		t.Errorf("user %s was equal to client %s, but should not have been", u1.GetName(), c.Name)
	}

	u.Delete()
	u2.Delete()
	c.Delete()
}
Beispiel #3
0
func createDefaultActors() {
	if cwebui, _ := client.Get("chef-webui"); cwebui == nil {
		if webui, nerr := client.New("chef-webui"); nerr != nil {
			logger.Criticalf(nerr.Error())
			os.Exit(1)
		} else {
			webui.Admin = true
			pem, err := webui.GenerateKeys()
			if err != nil {
				logger.Criticalf(err.Error())
				os.Exit(1)
			}
			if config.Config.UseAuth {
				if fp, ferr := os.Create(fmt.Sprintf("%s/%s.pem", config.Config.ConfRoot, webui.Name)); ferr == nil {
					fp.Chmod(0600)
					fp.WriteString(pem)
					fp.Close()
				} else {
					logger.Criticalf(ferr.Error())
					os.Exit(1)
				}
			}

			webui.Save()
		}
	}

	if cvalid, _ := client.Get("chef-validator"); cvalid == nil {
		if validator, verr := client.New("chef-validator"); verr != nil {
			logger.Criticalf(verr.Error())
			os.Exit(1)
		} else {
			validator.Validator = true
			pem, err := validator.GenerateKeys()
			if err != nil {
				logger.Criticalf(err.Error())
				os.Exit(1)
			}
			if config.Config.UseAuth {
				if fp, ferr := os.Create(fmt.Sprintf("%s/%s.pem", config.Config.ConfRoot, validator.Name)); ferr == nil {
					fp.Chmod(0600)
					fp.WriteString(pem)
					fp.Close()
				} else {
					logger.Criticalf(ferr.Error())
					os.Exit(1)
				}
			}
			validator.Save()
		}
	}

	if uadmin, _ := user.Get("admin"); uadmin == nil {
		if admin, aerr := user.New("admin"); aerr != nil {
			logger.Criticalf(aerr.Error())
			os.Exit(1)
		} else {
			admin.Admin = true
			pem, err := admin.GenerateKeys()
			if err != nil {
				logger.Criticalf(err.Error())
				os.Exit(1)
			}
			if config.Config.UseAuth {
				if fp, ferr := os.Create(fmt.Sprintf("%s/%s.pem", config.Config.ConfRoot, admin.Name)); ferr == nil {
					fp.Chmod(0600)
					fp.WriteString(pem)
					fp.Close()
				} else {
					logger.Criticalf(ferr.Error())
					os.Exit(1)
				}
			}
			if aerr := admin.Save(); aerr != nil {
				logger.Criticalf(aerr.Error())
				os.Exit(1)
			}
		}
	}

	environment.MakeDefaultEnvironment()

	return
}
Beispiel #4
0
func TestLogEvent(t *testing.T) {
	k := make(map[int]interface{})
	gob.Register(k)
	kk := new(LogInfo)
	gob.Register(kk)
	config.Config.LogEvents = true
	doer, _ := client.New("doer")
	obj, _ := client.New("obj")
	gob.Register(doer)
	err := LogEvent(doer, obj, "create")
	if err != nil {
		t.Errorf(err.Error())
	}
	ds := datastore.New()
	arr := ds.GetLogInfoList()
	if len(arr) != 1 {
		t.Errorf("Too many (or not enough) log events: %d found", len(arr))
	}
	arr2, _ := GetLogInfos(nil, 0, 1)
	if len(arr2) != 1 {
		t.Errorf("Something went wrong with variadic args with GetLogInfoList")
	}
	arr3, _ := GetLogInfos(nil)
	if len(arr3) != 1 {
		t.Errorf("Something went wrong with variadic args with no arguments with GetLogInfoList")
	}
	arr4, _ := GetLogInfos(nil, 0)
	if len(arr4) != 1 {
		t.Errorf("Something went wrong with variadic args with one argument with GetLogInfoList")
	}
	le := arr[1].(*LogInfo)
	if le.Action != "create" {
		t.Errorf("Wrong action")
	}
	if le.ActorType != "client" {
		t.Errorf("wrong actor type, got %s", le.ActorType)
	}
	if le.ObjectName != obj.GetName() {
		t.Errorf("wrong object")
	}
	var tdef time.Time
	if le.Time == tdef {
		t.Errorf("no time")
	}
	if le.ExtendedInfo == "" {
		t.Errorf("extended info did not get logged")
	}
	ds.DeleteLogInfo(1)
	arr5 := ds.GetLogInfoList()
	if len(arr5) != 0 {
		t.Errorf("Doesn't look like the logged event got deleted")
	}
	for i := 0; i < 10; i++ {
		LogEvent(doer, obj, "modify")
	}
	arr6 := ds.GetLogInfoList()
	if len(arr6) != 10 {
		t.Errorf("Something went wrong with creating 10 events")
	}
	ds.PurgeLogInfoBefore(5)
	arr7 := ds.GetLogInfoList()
	if len(arr7) != 5 {
		t.Errorf("Should have been 5 events after purging, got %d", len(arr7))
	}
	ds.PurgeLogInfoBefore(10)
	doer2, _ := client.New("doer2")
	for i := 0; i < 10; i++ {
		LogEvent(doer, obj, "modify")
		LogEvent(doer2, obj, "create")
	}
	searchParams := make(map[string]string)
	searchParams["doer"] = doer2.Name
	searching, err := GetLogInfos(searchParams, 0)
	if err != nil {
		t.Errorf(err.Error())
	}
	if len(searching) != 10 {
		t.Errorf("len(searching) for log events by doer2 should have returned 10 items, but returned %d instead", len(searching))
	}
}
Beispiel #5
0
func makeSearchItems() int {
	indexer.Initialize(config.Config)
	/* Gotta populate the search index */
	nodes := make([]*node.Node, 4)
	roles := make([]*role.Role, 4)
	envs := make([]*environment.ChefEnvironment, 4)
	clients := make([]*client.Client, 4)
	dbags := make([]*databag.DataBag, 4)
	gob.Register(new(node.Node))
	gob.Register(new(role.Role))
	gob.Register(new(environment.ChefEnvironment))
	gob.Register(new(client.Client))
	gob.Register(new(databag.DataBag))

	// circleci is sometimes weird about the index having everything. This
	// *never* comes up locally. ??? Could possibly be because the indexer
	// hasn't had a chance to finish indexing?
	reindexObjs := make([]indexer.Indexable, 0, 4*5)
	for i := 0; i < 4; i++ {
		nodes[i], _ = node.New(fmt.Sprintf("node%d", i))
		nodes[i].Default["baz"] = fmt.Sprintf("borb")
		nodes[i].Default["blurg"] = fmt.Sprintf("b%d", i)
		nodes[i].Save()
		roles[i], _ = role.New(fmt.Sprintf("role%d", i))
		roles[i].Save()
		envs[i], _ = environment.New(fmt.Sprintf("env%d", i))
		envs[i].Save()
		clients[i], _ = client.New(fmt.Sprintf("client%d", i))
		clients[i].Save()
		dbags[i], _ = databag.New(fmt.Sprintf("databag%d", i))
		dbags[i].Save()
		dbi := make(map[string]interface{})
		dbi["id"] = fmt.Sprintf("dbi%d", i)
		dbi["foo"] = fmt.Sprintf("dbag_item_%d", i)
		dbags[i].NewDBItem(dbi)
		reindexObjs = append(reindexObjs, nodes[i])
		reindexObjs = append(reindexObjs, roles[i])
		reindexObjs = append(reindexObjs, envs[i])
		reindexObjs = append(reindexObjs, clients[i])
		dbis, _ := dbags[i].AllDBItems()
		for _, d := range dbis {
			reindexObjs = append(reindexObjs, d)
		}
	}
	node1 = nodes[0]
	node2 = nodes[1]
	node3 = nodes[2]
	node4 = nodes[3]
	role1 = roles[0]
	role2 = roles[1]
	role3 = roles[2]
	role4 = roles[3]
	env1 = envs[0]
	env2 = envs[1]
	env3 = envs[2]
	env4 = envs[3]
	client1 = clients[0]
	client2 = clients[1]
	client3 = clients[2]
	client4 = clients[3]
	dbag1 = dbags[0]
	dbag2 = dbags[1]
	dbag3 = dbags[2]
	dbag4 = dbags[3]

	// Let the indexing functions catch up. This has not been a problem in
	// The Real World™ (famous last words), but it's *definitely* a problem
	// when running go test with GOMAXPROCS > 1.
	time.Sleep(1 * time.Second)

	/* Make this function return something so the compiler's happy building
	 * the tests. */
	return 1
}