|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "net" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "os" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +func setupMockServer() (*httptest.Server, string, int, error) { |
| 14 | + listener, err := net.Listen("tcp", ":0") |
| 15 | + if err != nil { |
| 16 | + return nil, "", 0, err |
| 17 | + } |
| 18 | + |
| 19 | + mux := http.NewServeMux() |
| 20 | + |
| 21 | + port := listener.Addr().(*net.TCPAddr).Port |
| 22 | + identityServerURL := fmt.Sprintf("http://localhost:%d", port) |
| 23 | + |
| 24 | + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 25 | + w.Header().Set("Content-Type", "application/json") |
| 26 | + w.WriteHeader(http.StatusOK) |
| 27 | + response := fmt.Sprintf(`{ |
| 28 | + "versions": { |
| 29 | + "values": [ |
| 30 | + { |
| 31 | + "id": "v3.0", |
| 32 | + "links": [ |
| 33 | + {"rel": "self", "href": "%s/v3/"} |
| 34 | + ], |
| 35 | + "status": "stable" |
| 36 | + } |
| 37 | + ] |
| 38 | + } |
| 39 | + }`, identityServerURL) |
| 40 | + fmt.Fprint(w, response) |
| 41 | + }) |
| 42 | + |
| 43 | + mux.HandleFunc("/v2/images/", func(w http.ResponseWriter, r *http.Request) { |
| 44 | + fmt.Fprintln(w, `mock_data`) |
| 45 | + }) |
| 46 | + |
| 47 | + mux.HandleFunc("/v3/auth/tokens", func(w http.ResponseWriter, r *http.Request) { |
| 48 | + w.Header().Set("Content-Type", "application/json") |
| 49 | + w.Header().Set("X-Subject-Token", "MIIFvgY") |
| 50 | + w.WriteHeader(http.StatusCreated) |
| 51 | + identityPublicURL := fmt.Sprintf("http://localhost:%d/v3/", port) |
| 52 | + identityAdminURL := fmt.Sprintf("http://localhost:%d/v3/", port) |
| 53 | + identityInternalURL := fmt.Sprintf("http://localhost:%d/v3/", port) |
| 54 | + imageServiceURL := fmt.Sprintf("http://localhost:%d/v2/images", port) |
| 55 | + |
| 56 | + response := fmt.Sprintf(`{ |
| 57 | + "token": { |
| 58 | + "methods": ["password"], |
| 59 | + "project": { |
| 60 | + "domain": { |
| 61 | + "id": "default", |
| 62 | + "name": "Default" |
| 63 | + }, |
| 64 | + "id": "8538a3f13f9541b28c2620eb19065e45", |
| 65 | + "name": "admin" |
| 66 | + }, |
| 67 | + "catalog": [ |
| 68 | + { |
| 69 | + "type": "identity", |
| 70 | + "name": "keystone", |
| 71 | + "endpoints": [ |
| 72 | + { |
| 73 | + "url": "%s", |
| 74 | + "region": "RegionOne", |
| 75 | + "interface": "public", |
| 76 | + "id": "identity-public-endpoint-id" |
| 77 | + }, |
| 78 | + { |
| 79 | + "url": "%s", |
| 80 | + "region": "RegionOne", |
| 81 | + "interface": "admin", |
| 82 | + "id": "identity-admin-endpoint-id" |
| 83 | + }, |
| 84 | + { |
| 85 | + "url": "%s", |
| 86 | + "region": "RegionOne", |
| 87 | + "interface": "internal", |
| 88 | + "id": "identity-internal-endpoint-id" |
| 89 | + } |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "type": "image", |
| 94 | + "name": "glance", |
| 95 | + "endpoints": [ |
| 96 | + { |
| 97 | + "url": "%s", |
| 98 | + "region": "RegionOne", |
| 99 | + "interface": "public", |
| 100 | + "id": "image-public-endpoint-id" |
| 101 | + } |
| 102 | + ] |
| 103 | + } |
| 104 | + ], |
| 105 | + "user": { |
| 106 | + "domain": { |
| 107 | + "id": "default", |
| 108 | + "name": "Default" |
| 109 | + }, |
| 110 | + "id": "3ec3164f750146be97f21559ee4d9c51", |
| 111 | + "name": "admin" |
| 112 | + }, |
| 113 | + "issued_at": "201406-10T20:55:16.806027Z" |
| 114 | + } |
| 115 | + }`, |
| 116 | + identityPublicURL, |
| 117 | + identityAdminURL, |
| 118 | + identityInternalURL, |
| 119 | + imageServiceURL) |
| 120 | + |
| 121 | + fmt.Fprint(w, response) |
| 122 | + }) |
| 123 | + |
| 124 | + server := httptest.NewUnstartedServer(mux) |
| 125 | + server.Listener = listener |
| 126 | + |
| 127 | + server.Start() |
| 128 | + |
| 129 | + return server, identityServerURL, port, nil |
| 130 | +} |
| 131 | + |
| 132 | +func TestPopulate(t *testing.T) { |
| 133 | + os.Setenv("username", "testuser") |
| 134 | + os.Setenv("password", "testpassword") |
| 135 | + os.Setenv("projectName", "Default") |
| 136 | + os.Setenv("domainName", "Default") |
| 137 | + os.Setenv("insecureSkipVerify", "true") |
| 138 | + os.Setenv("availability", "public") |
| 139 | + os.Setenv("regionName", "RegionOne") |
| 140 | + os.Setenv("authType", "password") |
| 141 | + |
| 142 | + server, identityServerURL, port, err := setupMockServer() |
| 143 | + if err != nil { |
| 144 | + t.Fatalf("Failed to start mock server: %v", err) |
| 145 | + } |
| 146 | + defer server.Close() |
| 147 | + |
| 148 | + fmt.Printf("Mock server running on port: %d\n", port) |
| 149 | + |
| 150 | + fileName := "disk.img" |
| 151 | + secretName := "test-secret" |
| 152 | + imageID := "test-image-id" |
| 153 | + |
| 154 | + fmt.Println("server ", identityServerURL) |
| 155 | + populate(fileName, identityServerURL, secretName, imageID) |
| 156 | + |
| 157 | + file, err := os.Open(fileName) |
| 158 | + if err != nil { |
| 159 | + t.Fatalf("Failed to open file: %v", err) |
| 160 | + } |
| 161 | + defer file.Close() // Ensure the file is closed after reading |
| 162 | + |
| 163 | + content, err := io.ReadAll(file) |
| 164 | + if err != nil { |
| 165 | + t.Fatalf("Failed to read file: %v", err) |
| 166 | + } |
| 167 | + |
| 168 | + if string(content) != "mock_data\n" { |
| 169 | + t.Errorf("Expected %s, got %s", "mock_data\n", string(content)) |
| 170 | + } |
| 171 | + |
| 172 | + os.Remove(fileName) |
| 173 | +} |
0 commit comments