|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License; |
| 3 | +// you may not use this file except in compliance with the Elastic License. |
| 4 | + |
| 5 | +package testing |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "net/http" |
| 11 | + "net/http/httptest" |
| 12 | + "testing" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +func TestHttpFetcher_Fetch(t *testing.T) { |
| 20 | + type fields struct { |
| 21 | + baseURL string |
| 22 | + } |
| 23 | + type args struct { |
| 24 | + operatingSystem string |
| 25 | + architecture string |
| 26 | + version string |
| 27 | + } |
| 28 | + tests := []struct { |
| 29 | + name string |
| 30 | + fields fields |
| 31 | + args args |
| 32 | + want FetcherResult |
| 33 | + wantErr assert.ErrorAssertionFunc |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "default elastic artifacts http fetcher", |
| 37 | + args: args{ |
| 38 | + operatingSystem: "linux", |
| 39 | + architecture: "arm64", |
| 40 | + version: "1.2.3", |
| 41 | + }, |
| 42 | + want: &httpFetcherResult{ |
| 43 | + baseURL: "https://artifacts.elastic.co/downloads/beats/elastic-agent/", |
| 44 | + packageName: "elastic-agent-1.2.3-linux-arm64.tar.gz", |
| 45 | + }, |
| 46 | + wantErr: assert.NoError, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "custom baseURL http fetcher", |
| 50 | + fields: fields{baseURL: "http://somehost.somedomain/some/path/here"}, |
| 51 | + args: args{ |
| 52 | + operatingSystem: "windows", |
| 53 | + architecture: "amd64", |
| 54 | + version: "1.2.3", |
| 55 | + }, |
| 56 | + want: &httpFetcherResult{ |
| 57 | + baseURL: "http://somehost.somedomain/some/path/here", |
| 58 | + packageName: "elastic-agent-1.2.3-windows-x86_64.zip", |
| 59 | + }, |
| 60 | + wantErr: assert.NoError, |
| 61 | + }, |
| 62 | + } |
| 63 | + for _, tt := range tests { |
| 64 | + t.Run(tt.name, func(t *testing.T) { |
| 65 | + var opts []HttpFetcherOpt |
| 66 | + if tt.fields.baseURL != "" { |
| 67 | + opts = append(opts, WithBaseURL(tt.fields.baseURL)) |
| 68 | + } |
| 69 | + h := NewHttpFetcher(opts...) |
| 70 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 71 | + defer cancel() |
| 72 | + got, err := h.Fetch(ctx, tt.args.operatingSystem, tt.args.architecture, tt.args.version) |
| 73 | + if !tt.wantErr(t, err, fmt.Sprintf("Fetch(%v, %v, %v, %v)", ctx, tt.args.operatingSystem, tt.args.architecture, tt.args.version)) { |
| 74 | + return |
| 75 | + } |
| 76 | + assert.Equalf(t, tt.want, got, "Fetch(%v, %v, %v, %v)", ctx, tt.args.operatingSystem, tt.args.architecture, tt.args.version) |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestHttpFetcher_Name(t *testing.T) { |
| 82 | + type fields struct { |
| 83 | + baseURL string |
| 84 | + } |
| 85 | + tests := []struct { |
| 86 | + name string |
| 87 | + fields fields |
| 88 | + want string |
| 89 | + }{ |
| 90 | + { |
| 91 | + name: "default elastic artifacts http fetcher", |
| 92 | + want: "httpFetcher-artifacts.elastic.co", |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "http fetcher with custom http url", |
| 96 | + fields: fields{baseURL: "http://somehost.somedomain:8888"}, |
| 97 | + want: "httpFetcher-somehost.somedomain", |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "http fetcher with base url not mantching regex", |
| 101 | + fields: fields{baseURL: "foo.bar-baz"}, |
| 102 | + want: "httpFetcher-foo.bar-baz", |
| 103 | + }, |
| 104 | + } |
| 105 | + for _, tt := range tests { |
| 106 | + t.Run(tt.name, func(t *testing.T) { |
| 107 | + var opts []HttpFetcherOpt |
| 108 | + if tt.fields.baseURL != "" { |
| 109 | + opts = append(opts, WithBaseURL(tt.fields.baseURL)) |
| 110 | + } |
| 111 | + h := NewHttpFetcher(opts...) |
| 112 | + assert.Equalf(t, tt.want, h.Name(), "Name()") |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func Test_httpFetcherResult_Fetch(t *testing.T) { |
| 118 | + type fields struct { |
| 119 | + packageName string |
| 120 | + } |
| 121 | + type args struct { |
| 122 | + availableFiles map[string]string |
| 123 | + } |
| 124 | + tests := []struct { |
| 125 | + name string |
| 126 | + fields fields |
| 127 | + args args |
| 128 | + wantErr assert.ErrorAssertionFunc |
| 129 | + }{ |
| 130 | + |
| 131 | + { |
| 132 | + name: "happy path linux package", |
| 133 | + fields: fields{packageName: "elastic-agent-1.2.3-linux-arm64.tar.gz"}, |
| 134 | + args: args{availableFiles: map[string]string{ |
| 135 | + "/elastic-agent-1.2.3-linux-arm64.tar.gz": "elastic-agent-package-placeholder", |
| 136 | + "/elastic-agent-1.2.3-linux-arm64.tar.gz.sha512": "elastic-agent-package hash", |
| 137 | + "/elastic-agent-1.2.3-linux-arm64.tar.gz.asc": "elastic-agent-package signature", |
| 138 | + }}, |
| 139 | + wantErr: assert.NoError, |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "linux package missing hash", |
| 143 | + fields: fields{packageName: "elastic-agent-1.2.3-linux-arm64.tar.gz"}, |
| 144 | + args: args{availableFiles: map[string]string{ |
| 145 | + "/elastic-agent-1.2.3-linux-arm64.tar.gz": "elastic-agent-package-placeholder", |
| 146 | + "/elastic-agent-1.2.3-linux-arm64.tar.gz.asc": "elastic-agent-package signature", |
| 147 | + }}, |
| 148 | + wantErr: assert.Error, |
| 149 | + }, |
| 150 | + { |
| 151 | + name: "windows package missing signature", |
| 152 | + fields: fields{packageName: "elastic-agent-1.2.3-windows-x86_64.zip"}, |
| 153 | + args: args{availableFiles: map[string]string{ |
| 154 | + "/elastic-agent-1.2.3-windows-x86_64.zip": "elastic-agent-package-placeholder", |
| 155 | + "/elastic-agent-1.2.3-windows-x86_64.zip.sha512": "elastic-agent-package hash", |
| 156 | + }}, |
| 157 | + wantErr: assert.Error, |
| 158 | + }, |
| 159 | + { |
| 160 | + name: "linux package missing completely", |
| 161 | + fields: fields{packageName: "elastic-agent-1.2.3-linux-arm64.tar.gz"}, |
| 162 | + args: args{availableFiles: map[string]string{}}, |
| 163 | + wantErr: assert.Error, |
| 164 | + }, |
| 165 | + } |
| 166 | + for _, tt := range tests { |
| 167 | + t.Run(tt.name, func(t *testing.T) { |
| 168 | + hf := http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { |
| 169 | + path := request.URL.Path |
| 170 | + content, ok := tt.args.availableFiles[path] |
| 171 | + if !ok { |
| 172 | + writer.WriteHeader(http.StatusNotFound) |
| 173 | + return |
| 174 | + } |
| 175 | + |
| 176 | + _, err := writer.Write([]byte(content)) |
| 177 | + require.NoError(t, err, "error writing file content") |
| 178 | + }) |
| 179 | + server := httptest.NewServer(hf) |
| 180 | + defer server.Close() |
| 181 | + h := httpFetcherResult{ |
| 182 | + baseURL: server.URL, |
| 183 | + packageName: tt.fields.packageName, |
| 184 | + } |
| 185 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 186 | + defer cancel() |
| 187 | + |
| 188 | + outdir := t.TempDir() |
| 189 | + tt.wantErr(t, h.Fetch(ctx, t, outdir), fmt.Sprintf("Fetch(%v, %v)", tt.fields.packageName, outdir)) |
| 190 | + }) |
| 191 | + } |
| 192 | +} |
0 commit comments