// Image examples.
func TestImageServiceScenarios(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)
	}

	imageService := image.NewService(authenticator)
	imagesDetails, err := imageService.ImagesDetail()
	if err != nil {
		t.Fatal("Cannot access images:", err)
	}

	var imageIDs = make([]string, 0)
	for _, element := range imagesDetails {
		imageIDs = append(imageIDs, element.ID)
	}

	if len(imageIDs) == 0 {
		t.Fatal("No images found, check to make sure access is correct")
	}
}
func testImageServiceHeadAction(t *testing.T, uriEndsWith string, testHeaders http.Header, testData []byte, imageServiceAction func(*image.Service)) {
	anon := func(req *http.Request) {
		reqURL := req.URL.String()
		if !strings.HasSuffix(reqURL, uriEndsWith) {
			t.Error(errors.New("Incorrect url created, expected:" + uriEndsWith + " at the end, actual url:" + reqURL))
		}
	}
	apiServer := testUtil.CreateHeadTestRequestServer(t, tokn, 200, testHeaders, testData, anon)
	defer apiServer.Close()

	imageService := image.NewService(common.SimpleAuthenticator{Token: tokn, ServiceURL: apiServer.URL})
	imageServiceAction(&imageService)
}
Exemple #3
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 TestImageV1ServiceCanQuery(t *testing.T) {
	_, _, _, authenticator := getServiceURLInitialize(t, "image")
	imageService := image.NewService(authenticator)
	_, err := imageService.Images()
	testUtil.IsNil(t, err)
}