From 6653745c801a132f57888db14a06c67cab405aa9 Mon Sep 17 00:00:00 2001 From: Benny Zlotnik Date: Sun, 3 Dec 2023 18:23:33 +0200 Subject: [PATCH] openstack: Add simple test for the openstack populator It uses a fake openstack server, but a real file is created Signed-off-by: Benny Zlotnik --- cmd/openstack-populator/BUILD.bazel | 8 +- .../openstack-populator_test.go | 173 ++++++++++++++++++ 2 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 cmd/openstack-populator/openstack-populator_test.go diff --git a/cmd/openstack-populator/BUILD.bazel b/cmd/openstack-populator/BUILD.bazel index 0ea7cbece..34f654219 100644 --- a/cmd/openstack-populator/BUILD.bazel +++ b/cmd/openstack-populator/BUILD.bazel @@ -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", @@ -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"], +) diff --git a/cmd/openstack-populator/openstack-populator_test.go b/cmd/openstack-populator/openstack-populator_test.go new file mode 100644 index 000000000..fc3700554 --- /dev/null +++ b/cmd/openstack-populator/openstack-populator_test.go @@ -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) +}