func TestDefaultServiceResolverURLNotFoundInServiceCatalogError(t *testing.T) {
	apiServer := testVersionList(t)
	defer apiServer.Close()
	url, err := defaultGetVersionURLFilterByVersion("t", nil, []service{}, "image", "region1", "1")
	testUtil.Equals(t, "", url)
	testUtil.Assert(t, err != nil, "Expected an error")
	testUtil.Equals(t, "ServiceCatalog does not contain serviceType 'image'", err.Error())
}
func TestDefaultServiceURLFoundInCatalogErrorsNotFoundInVersionList(t *testing.T) {
	apiServer := testVersionList(t)
	defer apiServer.Close()
	sc := []service{createService("image", "glance", endpoint{Region: "region1", PublicURL: apiServer.URL + "/publicurl", VersionList: apiServer.URL + "/versionlist"})}
	url, err := defaultGetVersionURLFilterByVersion("t", nil, sc, "image", "region1", "2")
	testUtil.Equals(t, "", url)
	testUtil.Assert(t, err != nil, "Expected an error")
	testUtil.Equals(t, "Found serviceType 'image' in the ServiceCatalog but cannot find an endpoint with the specified region 'region1' and version '2'", err.Error())
}
func TestGetVolumeTypes(t *testing.T) {
	apiServer := testUtil.CreateGetJSONTestRequestServerVerifyStatusAndURL(t, tokn, 200, sampleVolumeTypesJson, "/types")
	defer apiServer.Close()

	service := CreateVolumeService(apiServer.URL)
	volumes, err := service.VolumeTypes()
	testUtil.IsNil(t, err)
	testUtil.Assert(t, len(volumes) == 1, "Expected 1 volume")
	testUtil.Equals(t, volumeTypeSample, volumes[0])
}
func TestSecurityGroupScenarios(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test in short mode.")
	}

	authenticator, err := identity.AuthenticateFromEnvVars()
	if err != nil {
		t.Fatal("Cannot authenticate from env vars:", err)
	}

	computeService := compute.NewService(authenticator)

	sampleSecurityGroup := compute.CreateSecurityGroupParameters{
		Description: "Description",
		Name:        "NewCustomSecurityGroup",
	}

	securityGroup, err := computeService.CreateSecurityGroup(sampleSecurityGroup)
	if err != nil {
		t.Fatal("Cannot create security group:", err)
	}

	tcp := compute.TCP
	cidr := "0.0.0.0/0"
	sampleRule := compute.CreateSecurityGroupRuleParameters{
		FromPort:      80,
		ToPort:        80,
		IPProtocol:    tcp,
		CIDR:          &cidr,
		ParentGroupID: securityGroup.ID,
	}

	securityGroupRule, err := computeService.CreateSecurityGroupRule(sampleRule)
	if err != nil {
		t.Fatal("Cannot create security group rule:", err)
	}

	queriedSecurityGroup, err := computeService.SecurityGroup(securityGroup.ID)
	if err != nil {
		t.Fatal("Cannot requery security group:", err)
	}

	testUtil.Assert(t, len(queriedSecurityGroup.Rules) > 0, "Expected Security group to have a rule")

	err = computeService.DeleteSecurityGroupRule(securityGroupRule.ID)
	if err != nil {
		t.Fatal("Cannot delete security group rule:", err)
	}

	err = computeService.DeleteSecurityGroup(queriedSecurityGroup.ID)
	if err != nil {
		t.Fatal("Cannot delete security group:", err)
	}
}
func authenticate(t *testing.T) (string, common.Authenticator, common.AuthenticationParameters) {
	ap := getAuthenticationParameters(t)
	authenticator := identity.Authenticate(ap)
	authenticator.SetFunction(requester.DebugRequestMakerGenerator(nil, nil, testing.Verbose()))

	tokenID, err := authenticator.GetToken()
	testUtil.IsNil(t, err)
	testUtil.Assert(t, tokenID != "", "No tokenID was found.")

	return tokenID, &authenticator, ap
}
Ejemplo n.º 6
0
func TestQueryServersDetail(t *testing.T) {
	apiServer := testUtil.CreateGetJSONTestRequestServerVerifyURL(t, tokn, serversDetailJSONPayload, "/servers/detail?limit=21&name=foo")
	defer apiServer.Close()

	computeService := CreateComputeService(apiServer.URL)
	queryParameters := ServerDetailQueryParameters{Name: "foo"}
	servers, err := computeService.QueryServersDetail(queryParameters)

	testUtil.IsNil(t, err)
	testUtil.Assert(t, len(servers) == 1, "Expected 1 server.")
	testUtil.Equals(t, serverDetail, servers[0])
}
func getServiceURLInitialize(t *testing.T, serviceType string) (string, string, common.AuthenticationParameters, common.Authenticator) {
	if testing.Short() {
		t.Skip("skipping test in short mode.")
	}

	version, ok := currentRunningMap[serviceType]
	if !ok {
		t.Skip("skipping test as the service type is not in the map specifying the version to use.")
	}

	tokenID, authenticator, ap := authenticate(t)
	serviceURL, err := authenticator.GetServiceURL(serviceType, version)
	testUtil.IsNil(t, err)
	testUtil.Assert(t, serviceURL != "", "No service URL was found.")

	return tokenID, serviceURL, ap, authenticator
}
func TestFromFromEnvVarsShouldErrorWithNoAuthURLUserNameAndPassword(t *testing.T) {
	savedParams, _ := common.FromEnvVars()
	if savedParams.AuthURL != "" {
		t.Skip("Skipping testing as open stack environments are set.")
	}

	os.Setenv("OS_AUTH_URL", "")
	os.Setenv("OS_TENANT_ID", "")
	os.Setenv("OS_TENANT_NAME", "")
	os.Setenv("OS_USERNAME", "")
	os.Setenv("OS_PASSWORD", "")
	os.Setenv("OS_REGION_NAME", "")

	_, err := common.FromEnvVars()
	testUtil.Assert(t, err != nil, "Expected an error")
	testUtil.Equals(t, "No value provided for env variable 'OS_AUTH_URL'\nNo value provided for env variable 'OS_REGION_NAME'\nEither AuthToken or Username-Password should be provided", err.Error())

	applyParameter(savedParams)
}
// Image examples.
func TestDatabaseServiceScenarios(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test in short mode.")
	}

	if os.Getenv("OS_AUTH_URL") == "" {
		t.Skip("Cannot run integration test as the Openstack env vars aren't set.")
	}

	authenticator, err := identity.AuthenticateFromEnvVars()
	authenticator.(requester.Manager).SetFunction(requester.DebugRequestMakerGenerator(nil, nil, testing.Verbose()))
	if err != nil {
		t.Fatal("Cannot authenticate from env vars:", err)
	}

	databaseService := database.NewService(authenticator)

	user := database.UserParameter{Name: "User1", Password: "******"}
	db := database.CreateDatabaseParameters{Name: "newDb"}
	createInstanceParams := database.CreateInstanceParameters{
		Databases: []database.CreateDatabaseParameters{db},
		FlavorRef: "1001",
		Name:      "Instance1",
		Users:     []database.UserParameter{user},
	}

	instance, err := databaseService.CreateInstance(createInstanceParams)
	if err != nil {
		t.Fatal("Cannot create instance:", err)
	}

	WaitUntilActiveOrError(t, databaseService, instance.ID)

	foundInstance := false
	instances, err := databaseService.Instances()
	if err != nil {
		t.Fatal("Cannot query Instances:", err)
	}

	for _, i := range instances {
		if i.ID == instance.ID {
			foundInstance = true
			break
		}
	}

	if !foundInstance {
		t.Fatal("Cannot find new instance")
	}

	err = databaseService.CreateUser(instance.ID, database.UserParameter{Name: "username", Password: "******"})
	if err != nil {
		t.Fatal("Cannot create user", err)
	}

	err = databaseService.DeleteUser(instance.ID, "username")
	if err != nil {
		t.Fatal("Cannot delete user", err)
	}

	pwd, err := databaseService.EnableRoot(instance.ID)
	if err != nil {
		t.Fatal("Cannot enable root", err)
	}
	testUtil.Assert(t, pwd.Name != "", "No Name")

	backup, err := databaseService.CreateBackup(database.CreateBackupParameters{Name: "NewBackup", InstanceID: instance.ID, Description: "Test Description"})
	if err != nil {
		t.Fatal("Cannot make backup", err)
	}
	WaitUntilActiveOrError(t, databaseService, instance.ID)

	err = databaseService.DeleteInstance(instance.ID)
	if err != nil {
		t.Fatal("Delete instance didn't work:", err)
	}

	newInstance, err := databaseService.RestoreBackupInstance(database.RestoreBackupParameters{Name: "Instance1", RestorePoint: database.RestorePoint{BackupRef: backup.ID}, FlavorRef: "1001"})
	if err != nil {
		t.Fatal("Cannot restore a backup", err)
	}

	WaitUntilActiveOrError(t, databaseService, newInstance.ID)

	err = databaseService.DeleteBackup(backup.ID)
	if err != nil {
		t.Fatal("Cannot delete backup", err)
	}

	err = databaseService.DeleteInstance(newInstance.ID)
	if err != nil {
		t.Fatal("Delete instance didn't work:", err)
	}
}
// Added this test to ensure that its understood that
// only one specific format is supported at this time.
func TestUnmarshalInvalidDateTimeFormatTimeTest(t *testing.T) {
	val := timeTest{}
	err := json.Unmarshal([]byte("2014-09-29T14:44"), &val)
	testUtil.Assert(t, err != nil, "expected an error")
}
func TestUnmarshalInvalidDataFormatTimeTest(t *testing.T) {
	val := timeTest{}
	err := json.Unmarshal([]byte("something other than date time"), &val)
	testUtil.Assert(t, err != nil, "expected an error")
}
func TestServiceURLNetworkErrorsWhenNonExistant(t *testing.T) {
	url, err := getAppendVersionServiceURL("t", nil, []service{}, "network", "region1", "3")
	testUtil.Equals(t, "", url)
	testUtil.Assert(t, err != nil, "Expected an error")
	testUtil.Equals(t, "ServiceCatalog does not contain serviceType 'network'", err.Error())
}
func TestServiceURLComputeErrorsWhenNonExistant(t *testing.T) {
	url, err := getComputeServiceURL("t", nil, []service{}, "compute", "region1", "3")
	testUtil.Equals(t, "", url)
	testUtil.Assert(t, err != nil, "Expected an error")
	testUtil.Equals(t, "ServiceCatalog does not contain serviceType 'computev3'", err.Error())
}
func TestEqualIsTrue(t *testing.T) {
	isEqual := misc.NewDateTime(testTime).Equal(misc.NewDateTime(testTime))
	testUtil.Assert(t, isEqual, "expected values to be equal")
}
func TestAfterThanIsTrue(t *testing.T) {
	timeAfterOriginal := testTime.Add(duration250ms)
	isAfter := misc.NewDateTime(timeAfterOriginal).After(misc.NewDateTime(testTime))
	testUtil.Assert(t, isAfter, "expected value to be after")
}
func TestBeforeThanIsTrue(t *testing.T) {
	newTime := testTime.Add(duration250ms)
	isBefore := misc.NewDateTime(testTime).Before(misc.NewDateTime(newTime))
	testUtil.Assert(t, isBefore, "expected value to be before")
}