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 TestKeyPairScenarios(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)

	createdKeypair, err := computeService.CreateKeyPair("testkeyPairName", "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDx8nkQv/zgGgB4rMYmIf+6A4l6Rr+o/6lHBQdW5aYd44bd8JttDCE/F/pNRr0lRE+PiqSPO8nDPHw0010JeMH9gYgnnFlyY3/OcJ02RhIPyyxYpv9FhY+2YiUkpwFOcLImyrxEsYXpD/0d3ac30bNH6Sw9JD9UZHYcpSxsIbECHw== Generated by Nova")
	if err != nil {
		t.Fatal("Cannot create keypair:", err)
	}

	queriedKeyPair, err := computeService.KeyPair(createdKeypair.Name)
	if err != nil {
		t.Fatal("Cannot requery keypair:", err)
	}

	keyPairs, err := computeService.KeyPairs()
	if err != nil {
		t.Fatal("Cannot access keypairs:", err)
	}

	foundKeyPair := false
	for _, keyPairValue := range keyPairs {
		if queriedKeyPair.Name == keyPairValue.Name {
			foundKeyPair = true
			break
		}
	}

	if !foundKeyPair {
		t.Fatal("Cannot find keypair that was created.")
	}

	err = computeService.DeleteKeyPair(queriedKeyPair.Name)
	if err != nil {
		t.Fatal("Cannot delete keypair:", err)
	}
}
func TestAuthenticate(t *testing.T) {
	//Authenticate with Global envs
	ap := getAuthenticationParameters(t)

	authenticator := identity.Authenticate(ap)
	authenticator.SetFunction(requester.DebugRequestMakerGenerator(nil, nil, testing.Verbose()))

	tokenID, err := authenticator.GetToken()
	if tokenID == "" || err != nil {
		t.Fatal("Error while authenticating")
	}

	tokenBasedAp := common.AuthenticationParameters{
		AuthURL:    ap.AuthURL,
		TenantID:   ap.TenantID,
		TenantName: ap.TenantName,
		Region:     ap.Region,
		CACert:     ap.CACert,
		AuthToken:  tokenID,
	}

	tokenBasedAuthenticator := identity.Authenticate(tokenBasedAp)
	tokenBasedAuthenticator.SetFunction(requester.DebugRequestMakerGenerator(nil, nil, testing.Verbose()))

	otherToken, err := tokenBasedAuthenticator.GetToken()
	if otherToken == "" || err != nil {
		t.Fatal("Error while authenticating with token")
	}

	//Test if able to connect to compute, and get Flavors
	serviceURL, err := tokenBasedAuthenticator.GetServiceURL("compute", "2")
	if serviceURL == "" || err != nil {
		t.Fatal("Error while fetching Compute Service with Token")
	}

	computeService := compute.NewService(&tokenBasedAuthenticator)
	_, err = computeService.Flavors()
	if err != nil {
		t.Fatal("Error while fetching Compute flavors with Token")
	}
}
func TestFlavorScenarios(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)

	flavors, err := computeService.Flavors()
	if err != nil {
		t.Fatalf("Cannot get flavors: %v", err)
	}

	flavorsDetail, err := computeService.FlavorsDetail()
	if err != nil {
		t.Fatalf("Cannot query FlavorsDetail: %v", err)
	}

	testUtil.Equals(t, len(flavorsDetail), len(flavors))

	if len(flavors) > 0 {
		fmt.Println("FlavorID to query:", flavors[0].ID)
		queriedItem, err := computeService.FlavorDetail(flavors[0].ID)
		fmt.Println("Results1:", queriedItem, "Error:", err)
		fmt.Println("Expected:", flavors[0])
		if err != nil {
			t.Fatalf("Cannot requery single flavor: %v", err)
		}

		testUtil.Equals(t, flavorsDetail[0], queriedItem)
	}

}
func TestServerScenarios(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test in short mode.")
	}

	imageID := os.Getenv("TEST_IMAGEID")
	if imageID == "" {
		t.Skip("Cannot run test because Env Var 'TEST_IMAGEID' is not set")
	}

	keyPairName := os.Getenv("TEST_KEYPAIR_NAME")
	if keyPairName == "" {
		t.Skip("Cannot run test because Env Var 'TEST_KEYPAIR_NAME' is not set")
	}

	flavorRef := os.Getenv("TEST_FLAVOR")
	if flavorRef == "" {
		t.Skip("Cannot run test because Env Var 'TEST_FLAVOR' is not set")
	}

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

	computeService := compute.NewService(authenticator)

	// Create a server with the min required parameters
	minParametersServer := compute.ServerCreationParameters{
		Name:        "testName",
		ImageRef:    imageID,
		KeyPairName: keyPairName,
		FlavorRef:   flavorRef,
	}

	createQueryDeleteServer(t, computeService, minParametersServer)
}
Example #6
0
func initTask(c *cli.Context) {

	var err error

	config, err = readConfigFile(c.GlobalString(Config))
	if err != nil {
		log.Fatal(err.Error())
	}
	for k := range config.Nodes {
		config.OrderedNodeKeys = append(config.OrderedNodeKeys, k)
	}
	sort.Strings(config.OrderedNodeKeys)

	config.Log()

	log.Printf("%-20s - %s\n", AuthURLEnv, c.GlobalString(AuthURL))
	log.Printf("%-20s - %s\n", TenantIDEnv, c.GlobalString(TenantID))
	log.Printf("%-20s - %s\n", TenantNameEnv, c.GlobalString(TenantName))
	log.Printf("%-20s - %s\n", UsernameEnv, c.GlobalString(Username))
	log.Printf("%-20s - %s\n", RegionNameEnv, c.GlobalString(RegionName))
	log.Printf("%-20s - %s\n", AuthTokenEnv, c.GlobalString(AuthToken))
	log.Printf("%-20s - %s\n", CACertEnv, c.GlobalString(CACert))
	log.Printf("%-20s - %v\n", SkipSSLValidation, c.GlobalBool(SkipSSLValidation))
	log.Printf("%-20s - %v\n", Debug, c.GlobalBool(Debug))

	authParameters := common.AuthenticationParameters{
		AuthURL:    c.GlobalString(AuthURL),
		Username:   c.GlobalString(Username),
		Password:   c.GlobalString(Password),
		Region:     c.GlobalString(RegionName),
		TenantID:   c.GlobalString(TenantID),
		TenantName: c.GlobalString(TenantName),
		CACert:     c.GlobalString(CACert),
	}

	var transport *http.Transport

	if c.GlobalString(CACert) != "" {

		pemData, err := ioutil.ReadFile(c.GlobalString(CACert))
		if err != nil {
			log.Fatal(fmt.Sprintf("%-20s - %s %s\n", "error:", "Unable to load CA Certificate file", c.GlobalString(CACert)))
		}

		certPool := x509.NewCertPool()

		if !certPool.AppendCertsFromPEM(pemData) {
			log.Fatal(fmt.Sprintf("%-20s - %s %s\n", "error:", "Invalid CA Certificates in file", c.GlobalString(CACert)))
		}

		transport = &http.Transport{
			TLSClientConfig: &tls.Config{
				RootCAs:            certPool,
				InsecureSkipVerify: c.GlobalBool(SkipSSLValidation),
			},
		}
		misc.Transport(transport)

	} else {
		transport = &http.Transport{
			TLSClientConfig: &tls.Config{
				InsecureSkipVerify: c.GlobalBool(SkipSSLValidation),
			},
		}
		misc.Transport(transport)
	}

	authenticator := identity.Authenticate(authParameters)
	authenticator.SetFunction(requester.DebugRequestMakerGenerator(nil, &http.Client{Transport: transport}, c.GlobalBool(Debug)))

	token, err := authenticator.GetToken()
	if err != nil {
		log.Fatal(fmt.Sprintf("%-20s - %s\n", "error:", err.Error()))
	}
	log.Printf("%-20s - %s\n", "token", token)

	computeService = compute.NewService(&authenticator)

	networkService = network.NewService(&authenticator)

	imageService = image.NewService(&authenticator)

	keypair, err = computeService.KeyPair(config.SSHKey)
	if err != nil {
		log.Fatal(fmt.Sprintf("%-20s - %s %s %s\n", "error:", "get keypair", config.SSHKey, err.Error()))
	}

	var q = network.QueryParameters{Name: config.Network}
	networks, err := networkService.QueryNetworks(q)
	if err != nil {
		log.Fatal(fmt.Sprintf("%-20s - %s %s %s\n", "error:", "get network by name", config.Network, err.Error()))
	}

	netwrk, err = networkService.Network(networks[0].ID)
	if err != nil {
		log.Fatal(fmt.Sprintf("%-20s - %s %s %s\n", "error:", "getting network by id", networks[0].ID, err.Error()))
	}
	log.Printf("%-20s - %s\n", "network", netwrk.ID)

	subnets, err = networkService.Subnets()
	if err != nil {
		log.Fatal(fmt.Sprintf("%-20s - %s %s\n", "error:", "get subnets", err.Error()))
	}

	ports, err = networkService.Ports()
	if err != nil {
		log.Fatal(fmt.Sprintf("%-20s - %s %s\n", "error:", "get ports", err.Error()))
	}

	sort.Sort(PortByName(ports))

	servers, err = computeService.Servers()
	if err != nil {
		log.Fatal(fmt.Sprintf("%-20s - %s %s\n", "error:", "get servers", err.Error()))
	}

	sort.Sort(ServerByName(servers))

	flavors, err := computeService.Flavors()
	if err != nil {
		log.Fatal(fmt.Sprintf("%-20s - %s %s\n", "error:", "get flavors", err.Error()))
	}

	flavorMap = make(map[string]string)
	for _, p := range flavors {
		flavorMap[p.Name] = p.ID
	}
}
func TestComputeV2ServiceCanQuery(t *testing.T) {
	_, _, _, authenticator := getServiceURLInitialize(t, "compute")
	computeService := compute.NewService(authenticator)
	_, err := computeService.Flavors()
	testUtil.IsNil(t, err)
}