Skip to content

Commit

Permalink
Fetch Ironic node list after each e2e test
Browse files Browse the repository at this point in the history
Signed-off-by: Huy Mai <huy.mai@est.tech>
  • Loading branch information
mquhuy committed Nov 1, 2024
1 parent 8a6d953 commit 1c72ffe
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
4 changes: 4 additions & 0 deletions hack/ci-e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ rm /tmp/bmo-e2e.tar
# This IP is defined by the network we created above.
IP_ADDRESS="192.168.222.1"

# This IP is also defined by the network above, and is used consistently in all of
# our e2e overlays
export IRONIC_PROVISIONING_IP="192.168.222.199"

pushd "${REPO_ROOT}/test/createVM" || exit 1
go run main.go --yaml-source-file "${E2E_BMCS_CONF_FILE}"
popd
Expand Down
83 changes: 83 additions & 0 deletions test/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ package e2e
import (
"bytes"
"context"
"crypto/tls"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -447,6 +453,10 @@ func Logf(format string, a ...interface{}) {
fmt.Fprintf(GinkgoWriter, "INFO: "+format+"\n", a...)
}

func Errorf(format string, a ...interface{}) {
fmt.Fprintf(GinkgoWriter, "ERROR: "+format+"\n", a...)
}

// FlakeAttempt retries the given function up to attempts times.
func FlakeAttempt(attempts int, f func() error) error {
var err error
Expand Down Expand Up @@ -507,4 +517,77 @@ func DumpResources(ctx context.Context, clusterProxy framework.ClusterProxy, nam
LogPath: filepath.Join(artifactFolder, "clusters", clusterProxy.GetName(), "resources"),
})
dumpCRDS(ctx, clusterProxy.GetClient(), filepath.Join(artifactFolder, "crd"))
DumpIronicNodes(ctx, artifactFolder)
}

func DumpIronicNodes(ctx context.Context, artifactFolder string) {
ironicProvisioningIP, exists := os.LookupEnv("IRONIC_PROVISIONING_IP")
if !exists {
Logf("Running fixture tests, skip fetching ironic nodes.")
return
}
ironicURL := fmt.Sprintf("https://%s/v1/nodes", net.JoinHostPort(ironicProvisioningIP, "6385"))
username := os.Getenv("IRONIC_USERNAME")
password := os.Getenv("IRONIC_PASSWORD")

// Create HTTP client with TLS settings
tlsConfig := &tls.Config{
InsecureSkipVerify: true, // #nosec G402 Skip verification as we are using self-signed certificates
}
client := &http.Client{
Transport: &http.Transport{TLSClientConfig: tlsConfig},
}

// Create the request
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ironicURL, http.NoBody)
if err != nil {
Errorf("Failed to create request: %v", err)
return
}

// Set basic auth header
auth := base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
req.Header.Add("Authorization", "Basic "+auth)

// Make the request
resp, err := client.Do(req)
if err != nil {
Errorf("Failed to send request: %v", err)
return
}

if resp.StatusCode != http.StatusOK {
Errorf("Request failed with status: %d", resp.StatusCode)
return
}
defer resp.Body.Close()
// Read and output the response
body, err := io.ReadAll(resp.Body)
if err != nil {
Errorf("Failed to read response body: %v", err)
return
}

var logOutput bytes.Buffer

// Format the JSON with indentation
err = json.Indent(&logOutput, body, "", " ")
if err != nil {
Errorf("Error formatting JSON: %v", err)
return
}

file, err := os.Create(path.Join(artifactFolder, "ironic-nodes.json"))
if err != nil {
Errorf("Error creating file: %v", err)
return
}
defer file.Close()

// Write indented JSON to file
_, err = file.Write(logOutput.Bytes())
if err != nil {
Errorf("Error writing JSON to file: %v", err)
return
}
}

0 comments on commit 1c72ffe

Please sign in to comment.