Skip to content

Commit

Permalink
openstack: Add simple test for the openstack populator
Browse files Browse the repository at this point in the history
It uses a fake openstack server, but a real file is created

Signed-off-by: Benny Zlotnik <[email protected]>
  • Loading branch information
bennyz committed Dec 4, 2023
1 parent b3300a3 commit 6653745
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 1 deletion.
8 changes: 7 additions & 1 deletion cmd/openstack-populator/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
load(
"@io_bazel_rules_docker//container:container.bzl",
"container_image",
Expand Down Expand Up @@ -31,3 +31,9 @@ container_image(
files = [":openstack-populator"],
visibility = ["//visibility:public"],
)

go_test(
name = "openstack-populator_test",
srcs = ["openstack-populator_test.go"],
embed = [":openstack-populator_lib"],
)
173 changes: 173 additions & 0 deletions cmd/openstack-populator/openstack-populator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package main

import (
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"os"
"testing"
)

func setupMockServer() (*httptest.Server, string, int, error) {
listener, err := net.Listen("tcp", ":0")
if err != nil {
return nil, "", 0, err
}

mux := http.NewServeMux()

port := listener.Addr().(*net.TCPAddr).Port
identityServerURL := fmt.Sprintf("http://localhost:%d", port)

mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
response := fmt.Sprintf(`{
"versions": {
"values": [
{
"id": "v3.0",
"links": [
{"rel": "self", "href": "%s/v3/"}
],
"status": "stable"
}
]
}
}`, identityServerURL)
fmt.Fprint(w, response)
})

mux.HandleFunc("/v2/images/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `mock_data`)
})

mux.HandleFunc("/v3/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Subject-Token", "MIIFvgY")
w.WriteHeader(http.StatusCreated)
identityPublicURL := fmt.Sprintf("http://localhost:%d/v3/", port)
identityAdminURL := fmt.Sprintf("http://localhost:%d/v3/", port)
identityInternalURL := fmt.Sprintf("http://localhost:%d/v3/", port)
imageServiceURL := fmt.Sprintf("http://localhost:%d/v2/images", port)

response := fmt.Sprintf(`{
"token": {
"methods": ["password"],
"project": {
"domain": {
"id": "default",
"name": "Default"
},
"id": "8538a3f13f9541b28c2620eb19065e45",
"name": "admin"
},
"catalog": [
{
"type": "identity",
"name": "keystone",
"endpoints": [
{
"url": "%s",
"region": "RegionOne",
"interface": "public",
"id": "identity-public-endpoint-id"
},
{
"url": "%s",
"region": "RegionOne",
"interface": "admin",
"id": "identity-admin-endpoint-id"
},
{
"url": "%s",
"region": "RegionOne",
"interface": "internal",
"id": "identity-internal-endpoint-id"
}
]
},
{
"type": "image",
"name": "glance",
"endpoints": [
{
"url": "%s",
"region": "RegionOne",
"interface": "public",
"id": "image-public-endpoint-id"
}
]
}
],
"user": {
"domain": {
"id": "default",
"name": "Default"
},
"id": "3ec3164f750146be97f21559ee4d9c51",
"name": "admin"
},
"issued_at": "201406-10T20:55:16.806027Z"
}
}`,
identityPublicURL,
identityAdminURL,
identityInternalURL,
imageServiceURL)

fmt.Fprint(w, response)
})

server := httptest.NewUnstartedServer(mux)
server.Listener = listener

server.Start()

return server, identityServerURL, port, nil
}

func TestPopulate(t *testing.T) {
os.Setenv("username", "testuser")
os.Setenv("password", "testpassword")
os.Setenv("projectName", "Default")
os.Setenv("domainName", "Default")
os.Setenv("insecureSkipVerify", "true")
os.Setenv("availability", "public")
os.Setenv("regionName", "RegionOne")
os.Setenv("authType", "password")

server, identityServerURL, port, err := setupMockServer()
if err != nil {
t.Fatalf("Failed to start mock server: %v", err)
}
defer server.Close()

fmt.Printf("Mock server running on port: %d\n", port)

fileName := "disk.img"
secretName := "test-secret"
imageID := "test-image-id"

fmt.Println("server ", identityServerURL)
populate(fileName, identityServerURL, secretName, imageID)

file, err := os.Open(fileName)
if err != nil {
t.Fatalf("Failed to open file: %v", err)
}
defer file.Close() // Ensure the file is closed after reading

content, err := io.ReadAll(file)
if err != nil {
t.Fatalf("Failed to read file: %v", err)
}

if string(content) != "mock_data\n" {
t.Errorf("Expected %s, got %s", "mock_data\n", string(content))
}

os.Remove(fileName)
}

0 comments on commit 6653745

Please sign in to comment.