Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move platform detection to pkg/installation
- moved Platform matching methods to pkg/installation, where: - they make more sense - they can be tested without worrying about cyclic imports due to pkg/testutil. - rewrote some tests in platform_test.go to be shorter. - added a test for "no platforms specified" case in TestValidatePlugin. - added a check to verify-code-patterns that uses a heuristic to detect inline initializations of index.(Plugin|Platform) structs. Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
- Loading branch information
Showing
10 changed files
with
127 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2019 The Kubernetes Authors. | ||
// | ||
// 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 installation | ||
|
||
import ( | ||
"os" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
|
||
"sigs.k8s.io/krew/pkg/index" | ||
"sigs.k8s.io/krew/pkg/testutil" | ||
) | ||
|
||
func Test_osArch(t *testing.T) { | ||
inOS, inArch := runtime.GOOS, runtime.GOARCH | ||
outOS, outArch := osArch() | ||
if inOS != outOS { | ||
t.Fatalf("returned OS=%q; expected=%q", outOS, inOS) | ||
} | ||
if inArch != outArch { | ||
t.Fatalf("returned Arch=%q; expected=%q", outArch, inArch) | ||
} | ||
} | ||
func Test_osArch_override(t *testing.T) { | ||
customOS, customArch := "dragons", "metav1" | ||
os.Setenv("KREW_OS", customOS) | ||
os.Setenv("KREW_ARCH", customArch) | ||
defer func() { | ||
os.Unsetenv("KREW_ARCH") | ||
os.Unsetenv("KREW_OS") | ||
}() | ||
|
||
outOS, outArch := osArch() | ||
if customOS != outOS { | ||
t.Fatalf("returned OS=%q; expected=%q", outOS, customOS) | ||
} | ||
if customArch != outArch { | ||
t.Fatalf("returned Arch=%q; expected=%q", outArch, customArch) | ||
} | ||
} | ||
|
||
func Test_matchPlatform(t *testing.T) { | ||
const targetOS, targetArch = "foo", "amd64" | ||
matchingPlatform := testutil.NewPlatform().WithOSArch("foo", "amd64").V() | ||
nonMatchingPlatform := testutil.NewPlatform().WithOSArch("bar", "amd64").V() | ||
|
||
p, ok, err := matchPlatform([]index.Platform{matchingPlatform, nonMatchingPlatform}, targetOS, targetArch) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !ok { | ||
t.Fatal("failed to find a match") | ||
} | ||
if diff := cmp.Diff(p, matchingPlatform); diff != "" { | ||
t.Fatalf("got a different object from the matching platform:\n%s", diff) | ||
} | ||
|
||
_, ok, err = matchPlatform([]index.Platform{nonMatchingPlatform}, targetOS, targetArch) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if ok { | ||
t.Fatal("got a matching platform, but was not expecting") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters