|
| 1 | +package generator |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + cdx "github.com/CycloneDX/cyclonedx-go" |
| 13 | + "github.com/idlab-discover/AIBoMGen-cli/internal/builder" |
| 14 | + "github.com/idlab-discover/AIBoMGen-cli/internal/scanner" |
| 15 | + "github.com/idlab-discover/AIBoMGen-cli/internal/ui" |
| 16 | +) |
| 17 | + |
| 18 | +type failingBuilder struct { |
| 19 | + err error |
| 20 | +} |
| 21 | + |
| 22 | +func (f *failingBuilder) Build(builder.BuildContext) (*cdx.BOM, error) { |
| 23 | + return nil, f.err |
| 24 | +} |
| 25 | + |
| 26 | +type roundTripFunc func(*http.Request) (*http.Response, error) |
| 27 | + |
| 28 | +func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { |
| 29 | + return f(req) |
| 30 | +} |
| 31 | + |
| 32 | +func TestBuildPerDiscovery_FetchesMetadataAndBuilds(t *testing.T) { |
| 33 | + responses := []string{ |
| 34 | + `{"id":"hf-alpha","modelId":"hf-alpha","author":"org","pipeline_tag":"tag","library_name":"lib","tags":["t1"],"license":"mit","sha":"abc","downloads":1,"likes":1,"lastModified":"2024-01-01","createdAt":"2023-01-01","private":false,"usedStorage":1,"cardData":{"license":"mit"}}`, |
| 35 | + `{"id":"hf-beta","modelId":"hf-beta","author":"org","pipeline_tag":"tag","library_name":"lib","tags":["t2"],"license":"apache","sha":"def","downloads":2,"likes":2,"lastModified":"2024-02-01","createdAt":"2023-02-01","private":false,"usedStorage":2,"cardData":{"license":"apache"}}`, |
| 36 | + } |
| 37 | + |
| 38 | + origTransport := http.DefaultTransport |
| 39 | + var paths []string |
| 40 | + http.DefaultTransport = roundTripFunc(func(req *http.Request) (*http.Response, error) { |
| 41 | + idx := len(paths) |
| 42 | + if idx >= len(responses) { |
| 43 | + t.Fatalf("unexpected request #%d to %s", idx+1, req.URL) |
| 44 | + } |
| 45 | + if got, want := req.Header.Get("Authorization"), "Bearer test-token"; got != want { |
| 46 | + t.Fatalf("authorization header = %q, want %q", got, want) |
| 47 | + } |
| 48 | + paths = append(paths, req.URL.Path) |
| 49 | + body := io.NopCloser(strings.NewReader(responses[idx])) |
| 50 | + return &http.Response{StatusCode: http.StatusOK, Body: body, Header: make(http.Header)}, nil |
| 51 | + }) |
| 52 | + t.Cleanup(func() { http.DefaultTransport = origTransport }) |
| 53 | + |
| 54 | + discoveries := []scanner.Discovery{ |
| 55 | + {ID: "org-model", Path: "model.py"}, |
| 56 | + {Name: "beta"}, |
| 57 | + {}, |
| 58 | + } |
| 59 | + |
| 60 | + got, err := BuildPerDiscovery(discoveries, "test-token", 0) |
| 61 | + if err != nil { |
| 62 | + t.Fatalf("BuildPerDiscovery() error = %v", err) |
| 63 | + } |
| 64 | + if len(got) != len(discoveries) { |
| 65 | + t.Fatalf("results len = %d, want %d", len(got), len(discoveries)) |
| 66 | + } |
| 67 | + if len(paths) != 2 { |
| 68 | + t.Fatalf("expected 2 fetches, got %d", len(paths)) |
| 69 | + } |
| 70 | + if got[0].BOM == nil || got[0].BOM.Metadata == nil || got[0].BOM.Metadata.Component == nil { |
| 71 | + t.Fatalf("first BOM missing metadata/component") |
| 72 | + } |
| 73 | + if got[0].BOM.Metadata.Component.Name != "hf-alpha" { |
| 74 | + t.Fatalf("first component name = %q, want hf-alpha", got[0].BOM.Metadata.Component.Name) |
| 75 | + } |
| 76 | + if got[1].Discovery.Name != "beta" { |
| 77 | + t.Fatalf("second discovery preserved name, got %q", got[1].Discovery.Name) |
| 78 | + } |
| 79 | + if got[2].BOM.Metadata.Component.Name != "model" { |
| 80 | + t.Fatalf("third component default name = %q, want model", got[2].BOM.Metadata.Component.Name) |
| 81 | + } |
| 82 | + if !strings.Contains(paths[1], "beta") { |
| 83 | + t.Fatalf("second request path %q missing beta", paths[1]) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestBuildPerDiscovery_FetchErrorStillBuilds(t *testing.T) { |
| 88 | + origTransport := http.DefaultTransport |
| 89 | + http.DefaultTransport = roundTripFunc(func(req *http.Request) (*http.Response, error) { |
| 90 | + return nil, errors.New("boom") |
| 91 | + }) |
| 92 | + t.Cleanup(func() { http.DefaultTransport = origTransport }) |
| 93 | + |
| 94 | + discoveries := []scanner.Discovery{{ID: "err-model"}} |
| 95 | + got, err := BuildPerDiscovery(discoveries, "", 5*time.Second) |
| 96 | + if err != nil { |
| 97 | + t.Fatalf("BuildPerDiscovery() error = %v", err) |
| 98 | + } |
| 99 | + if len(got) != 1 { |
| 100 | + t.Fatalf("len = %d, want 1", len(got)) |
| 101 | + } |
| 102 | + if got[0].BOM == nil || got[0].BOM.Metadata == nil || got[0].BOM.Metadata.Component == nil { |
| 103 | + t.Fatalf("result bom missing metadata/component") |
| 104 | + } |
| 105 | + if got[0].BOM.Metadata.Component.Name != "err-model" { |
| 106 | + t.Fatalf("component name = %q, want err-model", got[0].BOM.Metadata.Component.Name) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestLogfWritesWithConfiguredLogger(t *testing.T) { |
| 111 | + ui.Init(true) |
| 112 | + |
| 113 | + var buf bytes.Buffer |
| 114 | + SetLogger(&buf) |
| 115 | + t.Cleanup(func() { SetLogger(nil) }) |
| 116 | + |
| 117 | + logf("model-x", "hello %s", "world") |
| 118 | + |
| 119 | + got := buf.String() |
| 120 | + for _, want := range []string{"Generator:", "model=model-x", "hello world"} { |
| 121 | + if !strings.Contains(got, want) { |
| 122 | + t.Fatalf("log output %q missing %q", got, want) |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func TestBuildPerDiscovery_PropagatesBuilderError(t *testing.T) { |
| 128 | + origFactory := newBOMBuilder |
| 129 | + newBOMBuilder = func() bomBuilder { |
| 130 | + return &failingBuilder{err: errors.New("builder boom")} |
| 131 | + } |
| 132 | + t.Cleanup(func() { newBOMBuilder = origFactory }) |
| 133 | + |
| 134 | + _, err := BuildPerDiscovery([]scanner.Discovery{{}}, "", time.Second) |
| 135 | + if err == nil || !strings.Contains(err.Error(), "builder boom") { |
| 136 | + t.Fatalf("expected builder error, got %v", err) |
| 137 | + } |
| 138 | +} |
0 commit comments