|
| 1 | +package oracle |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/quay/claircore/pkg/ovalutil" |
| 13 | +) |
| 14 | + |
| 15 | +func TestUpdaterSetDynamicDiscovery(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + ctx := context.Background() |
| 18 | + now := time.Now().Year() |
| 19 | + |
| 20 | + cases := []struct { |
| 21 | + name string |
| 22 | + entries []string |
| 23 | + wantYears []int |
| 24 | + wantErr bool |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "happy-path-two-years-dedupe-and-filter", |
| 28 | + entries: []string{ |
| 29 | + `<a href="com.oracle.elsa-` + strconv.Itoa(now) + `.xml.bz2">com.oracle.elsa-` + strconv.Itoa(now) + `.xml.bz2</a>`, |
| 30 | + `<a href="com.oracle.elsa-` + strconv.Itoa(now-5) + `.xml.bz2">com.oracle.elsa-` + strconv.Itoa(now-5) + `.xml.bz2</a>`, |
| 31 | + `<a href="com.oracle.elsa-` + strconv.Itoa(now-15) + `.xml.bz2">com.oracle.elsa-` + strconv.Itoa(now-15) + `.xml.bz2</a>`, |
| 32 | + `<a href="com.oracle.elsa-` + strconv.Itoa(now) + `.xml.bz2">com.oracle.elsa-` + strconv.Itoa(now) + `.xml.bz2</a>`, |
| 33 | + }, |
| 34 | + wantYears: []int{now, now - 5}, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "no-matches", |
| 38 | + entries: []string{`<a href="unrelated.txt">unrelated.txt</a>`}, |
| 39 | + wantYears: nil, |
| 40 | + wantErr: true, |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + for _, tt := range cases { |
| 45 | + tt := tt |
| 46 | + t.Run(tt.name, func(t *testing.T) { |
| 47 | + t.Parallel() |
| 48 | + body := `<html><body>` + strings.Join(tt.entries, "\n") + `</body></html>` |
| 49 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 50 | + w.WriteHeader(http.StatusOK) |
| 51 | + _, _ = w.Write([]byte(body)) |
| 52 | + })) |
| 53 | + defer srv.Close() |
| 54 | + |
| 55 | + // Configure factory with test URL/client. |
| 56 | + f := &Factory{} |
| 57 | + err := f.Configure(ctx, func(v any) error { |
| 58 | + if cfg, ok := v.(*FactoryConfig); ok { |
| 59 | + cfg.URL = strings.TrimSuffix(srv.URL, "/") + "/" |
| 60 | + } |
| 61 | + return nil |
| 62 | + }, srv.Client()) |
| 63 | + if err != nil { |
| 64 | + t.Fatalf("configure: %v", err) |
| 65 | + } |
| 66 | + |
| 67 | + us, err := f.UpdaterSet(ctx) |
| 68 | + if tt.wantErr { |
| 69 | + if err == nil { |
| 70 | + t.Fatalf("expected error, got nil") |
| 71 | + } |
| 72 | + return |
| 73 | + } |
| 74 | + if err != nil { |
| 75 | + t.Fatalf("UpdaterSet: %v", err) |
| 76 | + } |
| 77 | + ups := us.Updaters() |
| 78 | + if len(ups) != len(tt.wantYears) { |
| 79 | + t.Fatalf("unexpected updater count: got %d want %d", len(ups), len(tt.wantYears)) |
| 80 | + } |
| 81 | + want := map[string]bool{} |
| 82 | + for _, y := range tt.wantYears { |
| 83 | + want[strconv.Itoa(y)] = false |
| 84 | + } |
| 85 | + for _, u := range ups { |
| 86 | + up, ok := u.(*Updater) |
| 87 | + if !ok { |
| 88 | + t.Fatalf("unexpected updater type: %T", u) |
| 89 | + } |
| 90 | + n := up.Name() |
| 91 | + parts := strings.Split(n, "-") |
| 92 | + if len(parts) < 3 { |
| 93 | + t.Fatalf("unexpected updater name format: %q", n) |
| 94 | + } |
| 95 | + yr := parts[1] |
| 96 | + if _, ok := want[yr]; !ok { |
| 97 | + t.Fatalf("unexpected year in updater name: %q", n) |
| 98 | + } |
| 99 | + want[yr] = true |
| 100 | + // URL and compression |
| 101 | + base := strings.TrimSuffix(srv.URL, "/") + "/" |
| 102 | + if up.Fetcher.URL == nil { |
| 103 | + t.Fatalf("nil URL for updater %q", n) |
| 104 | + } |
| 105 | + if !strings.HasPrefix(up.Fetcher.URL.String(), base+`com.oracle.elsa-`) || |
| 106 | + !strings.HasSuffix(up.Fetcher.URL.String(), `.xml.bz2`) { |
| 107 | + t.Fatalf("unexpected URL: %q", up.Fetcher.URL) |
| 108 | + } |
| 109 | + if up.Fetcher.Compression != ovalutil.CompressionBzip2 { |
| 110 | + t.Fatalf("unexpected compression: got %v want %v", up.Fetcher.Compression, ovalutil.CompressionBzip2) |
| 111 | + } |
| 112 | + } |
| 113 | + for yr, ok := range want { |
| 114 | + if !ok { |
| 115 | + t.Fatalf("missing updater for year %s", yr) |
| 116 | + } |
| 117 | + } |
| 118 | + }) |
| 119 | + } |
| 120 | +} |
0 commit comments