Skip to content

Commit b84e48a

Browse files
committed
test: add multirepo_test.go
Signed-off-by: Marvin Drees <[email protected]>
1 parent 0b2c739 commit b84e48a

File tree

2 files changed

+97
-3
lines changed

2 files changed

+97
-3
lines changed

metadata/multirepo/multirepo.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package multirepo
1919

2020
import (
2121
"encoding/json"
22+
"errors"
2223
"fmt"
2324
"os"
2425
"path/filepath"
@@ -29,6 +30,13 @@ import (
2930
"github.com/theupdateframework/go-tuf/v2/metadata/updater"
3031
)
3132

33+
var (
34+
// ErrNoMapFile is returned when no map file is provided
35+
ErrNoMapFile = errors.New("no map file provided")
36+
// ErrNoTrustedRoots is returned when no trusted root metadata is provided
37+
ErrNoTrustedRoots = errors.New("no trusted root metadata provided")
38+
)
39+
3240
// The following represent the map file described in TAP 4
3341
type Mapping struct {
3442
Paths []string `json:"paths"`
@@ -66,21 +74,21 @@ type targetMatch struct {
6674
func NewConfig(repoMap []byte, roots map[string][]byte) (*MultiRepoConfig, error) {
6775
// error if we don't have the necessary arguments
6876
if len(repoMap) == 0 || len(roots) == 0 {
69-
return nil, fmt.Errorf("failed to create multi-repository config: no map file and/or trusted root metadata is provided")
77+
return nil, fmt.Errorf("failed to create multi-repository config: %w and/or %w", ErrNoMapFile, ErrNoTrustedRoots)
7078
}
7179

7280
// unmarshal the map file (note: should we expect/support unrecognized values here?)
7381
var mapFile *MultiRepoMapType
7482
if err := json.Unmarshal(repoMap, &mapFile); err != nil {
75-
return nil, err
83+
return nil, fmt.Errorf("failed to unmarshal map file: %w", err)
7684
}
7785

7886
// make sure we have enough trusted root metadata files provided based on the repository list
7987
for repo := range mapFile.Repositories {
8088
// check if we have a trusted root metadata for this repository
8189
_, ok := roots[repo]
8290
if !ok {
83-
return nil, fmt.Errorf("no trusted root metadata provided for repository - %s", repo)
91+
return nil, fmt.Errorf("%w for repository - %s", ErrNoTrustedRoots, repo)
8492
}
8593
}
8694

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright 2024 The Update Framework Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License
14+
//
15+
// SPDX-License-Identifier: Apache-2.0
16+
//
17+
18+
package multirepo
19+
20+
import (
21+
"testing"
22+
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
func TestNewConfig(t *testing.T) {
27+
for _, tt := range []struct {
28+
name string
29+
desc string
30+
repoMap []byte
31+
roots map[string][]byte
32+
config *MultiRepoConfig
33+
wantErr error
34+
}{
35+
{
36+
name: "Success",
37+
desc: "This tests expects no error when creating a new config",
38+
repoMap: []byte(""),
39+
roots: map[string][]byte{},
40+
config: &MultiRepoConfig{},
41+
wantErr: nil,
42+
},
43+
} {
44+
t.Run(tt.name, func(t *testing.T) {
45+
t.Logf("Desc: %s", tt.desc)
46+
47+
cfg, err := NewConfig(tt.repoMap, tt.roots)
48+
49+
if tt.wantErr == nil {
50+
assert.NoErrorf(t, err, "expected no error but got %v", err)
51+
return
52+
}
53+
54+
assert.ErrorIsf(t, err, tt.wantErr, "expected error %v but got %v", tt.wantErr, err)
55+
assert.Equalf(t, tt.config, cfg, "expected config %v but got %v", tt.config, cfg)
56+
})
57+
}
58+
}
59+
60+
func TestNew(t *testing.T) {
61+
for _, tt := range []struct {
62+
name string
63+
desc string
64+
config *MultiRepoConfig
65+
client *MultiRepoClient
66+
wantErr error
67+
}{
68+
{
69+
name: "",
70+
},
71+
} {
72+
t.Run(tt.name, func(t *testing.T) {
73+
t.Logf("Desc: %s", tt.desc)
74+
75+
client, err := New(tt.config)
76+
77+
if tt.wantErr == nil {
78+
assert.NoErrorf(t, err, "expected no error but got %v", err)
79+
return
80+
}
81+
82+
assert.ErrorIsf(t, err, tt.wantErr, "expected error %v but got %v", tt.wantErr, err)
83+
assert.Equalf(t, tt.client, client, "expected client %v but got %v", tt.client, client)
84+
})
85+
}
86+
}

0 commit comments

Comments
 (0)