package middlechild import ( "fmt" "os" "sort" "testing" "time" "github.com/google/uuid" "gotest.tools/v3/assert" "edge-infra.dev/pkg/f8n/devinfra/testinfra/model" ) func TestGetArgoPodName(t *testing.T) { tfe := []struct { job *Job found bool pod []string }{ { &Job{ Metadata: []model.EdgeJobMetadata{ {Key: "some", Value: "a"}, }, }, false, []string{}, }, { &Job{ Metadata: []model.EdgeJobMetadata{ {Key: "some", Value: "a"}, {Key: "ID", Value: "hourly-l2-serial-test-1725372000-2227150978"}, }, }, true, []string{"hourly-l2-serial-test-1725372000-rosa-2227150978", "hourly-l2-serial-test-1725372000-rosa-with-kubeconfig-2227150978"}, }, { &Job{ Metadata: []model.EdgeJobMetadata{ {Key: "some", Value: "a"}, {Key: "ID", Value: ""}, }, }, true, []string{"", ""}, }, { &Job{ Metadata: []model.EdgeJobMetadata{ {Key: "some", Value: "a"}, {Key: "ID", Value: "1-2"}, }, }, true, []string{"1-rosa-2", "1-rosa-with-kubeconfig-2"}, }, { &Job{ Metadata: []model.EdgeJobMetadata{ {Key: "some", Value: "a"}, {Key: "ID", Value: "12"}, }, }, true, []string{"12", "12"}, }, } for i, tc := range tfe { t.Run(fmt.Sprintf("%s_%d", t.Name(), i), func(t *testing.T) { found, pod := getArgoPodName(tc.job) assert.Equal(t, tc.found, found) if tc.found { assert.DeepEqual(t, tc.pod, pod) } }) } } func TestBuildArgoLogPath(t *testing.T) { tfe := []struct { job *Job id string path string }{ { &Job{ JobData: model.EdgeJob{ Run: "12345678-abcd", Started: time.Unix(1725887349, 0).UTC(), }, }, "hourly-l2-serial-test-1-rosa-2", "2024/09/09/12345678-abcd/hourly-l2-serial-test-1-rosa-2/main.log", }, { &Job{ JobData: model.EdgeJob{ Run: "12345678-abcd", Started: time.Unix(1725973749, 0).UTC(), }, }, "hourly-l2-serial-test-1-rosa-2", "2024/09/10/12345678-abcd/hourly-l2-serial-test-1-rosa-2/main.log", }, } for i, tc := range tfe { t.Run(fmt.Sprintf("%s_%d", t.Name(), i), func(t *testing.T) { path := buildArgoLogPath(tc.job, tc.id) assert.Equal(t, tc.path, path) }) } } func TestBuildArgoResultPath(t *testing.T) { tfe := []struct { job *Job path string }{ { &Job{ JobData: model.EdgeJob{ Workflow: "hourly-slow-l2-test", Number: "39998649-1be4-4103-af4a-6f18f1f19aea-AE85iO", }, }, "argo/edge-infra/hourly-slow-l2-test/39998649-1be4-4103-af4a-6f18f1f19aea-AE85iO/logs/logs.txt", }, } for i, tc := range tfe { t.Run(fmt.Sprintf("%s_%d", t.Name(), i), func(t *testing.T) { path := buildArgoResultPath(tc.job) assert.Equal(t, tc.path, path) }) } } func TestGatherFinished(t *testing.T) { fjson := []byte(`{"timestamp":1635278696, "passed": true,"metadata":{"job_name":"build","workflow_name":"CI", "run_id": "2053035745", "platform": "actions"}}`) mc := MiddleChild{} mcj := &Job{} mcj.files.JSON = make(map[string][]byte) mcj.files.JSON["finished.json"] = fjson assert.NilError(t, mc.gatherFinished(mcj), "failed to gather finished data") t.Logf("%+v", mcj.Metadata) b := []byte{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, } uuid, err := uuid.FromBytes(b) assert.NilError(t, err, "failed to create uuid") meta := []model.EdgeJobMetadata{ { EdgeJob: uuid, Key: "job_name", Value: "build", }, { EdgeJob: uuid, Key: "workflow_name", Value: "CI", }, { EdgeJob: uuid, Key: "run_id", Value: "2053035745", }, { EdgeJob: uuid, Key: "platform", Value: "actions", }, } // sort the two structs because theyre not always equal sort.Slice(meta, func(i, j int) bool { return meta[i].Key < meta[j].Key }) sort.Slice(mcj.Metadata, func(i, j int) bool { return mcj.Metadata[i].Key < mcj.Metadata[j].Key }) assert.DeepEqual(t, meta, mcj.Metadata) } // func TestGatherJSON(t *testing.T) { // fjson := []byte(`{"timestamp":1635278696, "passed": true,"metadata":{"job_name":"build","workflow_name":"CI", "run_id": "2053035745"}}`) // sjson := []byte(`{"timestamp":1635278593,"machine":"edge-runner","pull":"1244","commit":"da5057b453da76a40f9b65a4e97303fb4f9bc10a","repo":"edge-infra","version":"0.2.0--62135596800"}`) // mcj := &Job{} // mcj.files.JSON = make(map[string][]byte) // mcj.files.JSON["finished.json"] = fjson // mcj.files.JSON["started.json"] = sjson // err := mcj.gatherStarted() // if err != nil { // t.Error("Failed to unmarshal started json into JobData struct", err) // } // st, _ := time.Parse(time.RFC3339, "2021-10-26T16:03:13-04:00") // ft, _ := time.Parse(time.RFC3339, "0001-01-01T00:00:00-04:00") // var testArr = bigquery.JobData{ // Elapsed: 0, // Started: st.UTC(), // Finished: ft.UTC(), // Version: "0.2.0--62135596800", // Path: "", // Job: "CI/build", // Number: 2053035745, // Metadata: []bigquery.Metadata{ // {Key: "pull", Value: "1244"}, // }, // TestsRun: 0, // TestsFailed: 0, // Passed: false, // Repos: "edge-infra", // RepoCommit: "da5057b453da76a40f9b65a4e97303fb4f9bc10a", // } // if reflect.DeepEqual(mcj.JobData, testArr) { // t.Error("Test list is incorrect", err) // } // err = mcj.gatherFinished() // if err != nil { // t.Error("Failed to unmarshal finished json into JobData struct", err) // } // ft, _ = time.Parse(time.RFC3339, "2021-10-26T16:04:56-04:00") // testArr = bigquery.JobData{ // Elapsed: 103, // Started: st.UTC(), // Finished: ft.UTC(), // Version: "0.2.0--62135596800", // Path: "", // Job: "CI/build", // Number: 2053035745, // Metadata: []bigquery.Metadata{ // {Key: "pull", Value: "1244"}, // {Key: "job_name", Value: "build"}, // {Key: "workflow_name", Value: "CI"}, // }, // TestsRun: 0, // TestsFailed: 0, // Passed: true, // Repos: "edge-infra", // RepoCommit: "da5057b453da76a40f9b65a4e97303fb4f9bc10a", // } // if !reflect.DeepEqual(mcj.JobData, testArr) { // t.Error("Test list is incorrect", err) // } // } func TestParseJUnit(t *testing.T) { tfe := []struct { path string tests int failed int checkJob bool job []model.EdgeJobTest }{ { "testdata/unit2558878622.xml", 47, 3, false, []model.EdgeJobTest{}, }, { "testdata/unit2598106678.xml", 82, 5, false, []model.EdgeJobTest{}, }, { "testdata/unit1027837568.xml", 18, 0, false, []model.EdgeJobTest{}, }, { "testdata/unit540587877.xml", 21, 0, false, []model.EdgeJobTest{}, }, { "testdata/passed.xml", 3, 0, true, []model.EdgeJobTest{ {Name: "TestDeploymentToLivenessReadinessResponse", Time: 0, Failed: false, FailureText: "", Suite: "edge-infra.dev/pkg/edge/api/graph/mapper"}, {Name: "TestDeploymentToLivenessReadinessResponseDaemonSet", Time: 0, Failed: false, FailureText: "", Suite: "edge-infra.dev/pkg/edge/api/graph/mapper"}, {Name: "TestIsClusterReadyForDeletedCluster", Time: 0, Failed: false, FailureText: "", Suite: "edge-infra.dev/pkg/edge/api/graph/mapper"}, }, }, { "testdata/failed.xml", 3, 1, true, []model.EdgeJobTest{ {Name: "TestDeploymentToLivenessReadinessResponse", Time: 0, Failed: true, FailureText: "Assertion failed", Suite: "edge-infra.dev/pkg/edge/api/graph/mapper"}, {Name: "TestDeploymentToLivenessReadinessResponseDaemonSet", Time: 0, Failed: false, FailureText: "", Suite: "edge-infra.dev/pkg/edge/api/graph/mapper"}, {Name: "TestIsClusterReadyForDeletedCluster", Time: 0, Failed: false, FailureText: "", Suite: "edge-infra.dev/pkg/edge/api/graph/mapper"}, }, }, } for i, tc := range tfe { t.Run(fmt.Sprintf("%s_%d", t.Name(), i), func(t *testing.T) { dat, err := os.ReadFile(tc.path) assert.NilError(t, err, "failed to read xml file") job, tests, failed, err := parseJUnit(dat) assert.NilError(t, err, "failed to parse junit") assert.Equal(t, tc.tests, tests) assert.Equal(t, tc.failed, failed) if tc.checkJob { assert.DeepEqual(t, tc.job, job) } }) } }