-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
2 changed files
with
180 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |