Skip to content

Commit

Permalink
pkg/utils: Test ImageReferenceCanBeID
Browse files Browse the repository at this point in the history
  • Loading branch information
HarryMichal authored and debarshiray committed Jul 7, 2021
1 parent 781c433 commit b27b41e
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright © 2021 Red Hat Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package utils

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestImageReferenceCanBeID(t *testing.T) {
testCases := []struct {
name string
ref string
ok bool
}{
{
name: "Valid ID (random 6 chars)",
ref: "34afbc",
ok: true,
},
{
name: "Valid ID (random 64 chars)",
ref: "8215cb84fa588215cb84fa588215cb84fa588215cb84fa588215cb84fa58fbca",
ok: true,
},
{
name: "Valid ID (Podman short)",
ref: "8215cb84fa58",
ok: true,
},
{
name: "Valid ID (Podman long)",
ref: "8b9affd1dbc261a7f586ed06a8fd993d09449a5ac79ebc7e80e86efdf3c223f6",
ok: true,
},
{
name: "Invalid ID (random <6 chars)",
ref: "acbdf",
ok: false,
},
{
name: "Invalid ID (random >64 chars)",
ref: "8215cb84fa588215cb84fa588215cb84fa588215cb84fa588215cb84fa58fbcab",
ok: false,
},
{
name: "Invalid ID (image URI; whole alphabet + slash)",
ref: "fedoraproject.org/fedora-toolbox:32",
ok: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ok := ImageReferenceCanBeID(tc.ref)
assert.Equal(t, tc.ok, ok)
})
}
}

0 comments on commit b27b41e

Please sign in to comment.