Change:
go test ./cmd/kubeadm/... fails with: it appears to be following a "RunCmd" pattern used by e2e tests, but unit tests should not require built binaries
can work on this
has a long discussion as to why this is. These tests only get run when gets run, which will produce the needed binary and pass it in as arg. For more information, authored this and should be able to provide more details.
Yup, the target explicitly ignores these tests: and like said, runs this script as part of its target:
then the test should have build tags to exclude it from a regular run of the package
or up for fixing that?
I thought build tags only works on the OS level or if you it would ignore it completely? Wouldnt break the command as well?
I'd also tolerate skipping the test if the built binary doesn't exist
I think that is a great solution. I think in I had initially suggested something like that. If everyone is ok with that, I will go ahead and make that change.
Sounds good, make that change and we'll see how it turns out. I'd really want these tests start working, so +1

Discussion:
srcs = [ ":package-srcs", "//cmd/kubeadm/app:all-srcs", <del> "//cmd/kubeadm/test:all-srcs", </del> <ins> "//cmd/kubeadm/test/cmd:all-srcs", </ins> ], tags = ["automanaged"], )

<del> package(default_visibility = ["//visibility:public"]) licenses(["notice"]) load( "@io_bazel_rules_go//go:def.bzl", "go_library", "go_test", ) go_library( name = "go_default_library", srcs = ["util.go"], tags = ["automanaged"], ) go_test( name = "go_default_test", srcs = ["token_test.go"], args = ["--kubeadm-path=../kubeadm"], data = ["//cmd/kubeadm"], library = ":go_default_library", tags = [ "automanaged", "integration", "skip", ], ) filegroup( name = "package-srcs", srcs = glob(["**"]), tags = ["automanaged"], visibility = ["//visibility:private"], ) filegroup( name = "all-srcs", srcs = [":package-srcs"], tags = ["automanaged"], ) </del>

<ins> package(default_visibility = ["//visibility:public"]) licenses(["notice"]) load( "@io_bazel_rules_go//go:def.bzl", "go_library", "go_test", ) go_library( name = "go_default_library", srcs = ["util.go"], tags = ["automanaged"], ) go_test( name = "go_default_test", srcs = ["token_test.go"], args = ["--kubeadm-path=../../kubeadm"], data = ["//cmd/kubeadm"], library = ":go_default_library", tags = [ "automanaged", "integration", "skip", ], ) filegroup( name = "package-srcs", srcs = glob(["**"]), tags = ["automanaged"], visibility = ["//visibility:private"], ) filegroup( name = "all-srcs", srcs = [":package-srcs"], tags = ["automanaged"], ) </ins>

<ins> /* Copyright 2016 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 kubeadm import ( "flag" "os" "path/filepath" "regexp" "testing" ) const ( TokenExpectedRegex = "^S{6}:S{16}n$" ) var kubeadmPath = flag.String("kubeadm-path", filepath.Join(os.Getenv("KUBE_ROOT"), "cluster/kubeadm.sh"), "Location of kubeadm") var kubeadmCmdSkip = flag.Bool("kubeadm-cmd-skip", false, "Skip kubeadm cmd tests") func TestCmdTokenGenerate(t *testing.T) { if *kubeadmCmdSkip { t.Log("kubeadm cmd tests being skipped") t.Skip() } stdout, _, err := RunCmd(*kubeadmPath, "ex", "token", "generate") if err != nil { t.Fatalf("'kubeadm ex token generate' exited uncleanly: %v", err) } matched, err := regexp.MatchString(TokenExpectedRegex, stdout) if err != nil { t.Fatalf("encountered an error while trying to match 'kubeadm ex token generate' stdout: %v", err) } if !matched { t.Errorf("'kubeadm ex token generate' stdout did not match expected regex; wanted: [%q], got: [%s]", TokenExpectedRegex, stdout) } } func TestCmdTokenGenerateTypoError(t *testing.T) { /* Since we expect users to do things like this: $ TOKEN=$(kubeadm ex token generate) we want to make sure that if they have a typo in their command, we exit with a non-zero status code after showing the command's usage, so that the usage itself isn't captured as a token without the user noticing. */ if *kubeadmCmdSkip { t.Log("kubeadm cmd tests being skipped") t.Skip() } _, _, err := RunCmd(*kubeadmPath, "ex", "token", "genorate") // subtle typo if err == nil { t.Error("'kubeadm ex token genorate' (a deliberate typo) exited without an error when we expected non-zero exit status") } } </ins>

<ins> /* Copyright 2016 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 kubeadm import ( "bytes" "fmt" "io" "os" "os/exec" ) // Forked from test/e2e/framework because the e2e framework is quite bloated // for our purposes here, and modified to remove undesired logging. func RunCmd(command string, args ...string) (string, string, error) { var bout, berr bytes.Buffer cmd := exec.Command(command, args...) cmd.Stdout = io.MultiWriter(os.Stdout, &bout) cmd.Stderr = io.MultiWriter(os.Stderr, &berr) err := cmd.Run() stdout, stderr := bout.String(), berr.String() if err != nil { return "", "", fmt.Errorf("error running %s %v; got error %v, stdout %q, stderr %q", command, args, err, stdout, stderr) } return stdout, stderr, nil } </ins>

<del> /* Copyright 2016 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 kubeadm import ( "flag" "os" "path/filepath" "regexp" "testing" ) const ( TokenExpectedRegex = "^S{6}:S{16}n$" ) var kubeadmPath string func init() { flag.StringVar(&kubeadmPath, "kubeadm-path", filepath.Join(os.Getenv("KUBE_ROOT"), "cluster/kubeadm.sh"), "Location of kubeadm") } func TestCmdTokenGenerate(t *testing.T) { stdout, _, err := RunCmd(kubeadmPath, "ex", "token", "generate") if err != nil { t.Fatalf("'kubeadm ex token generate' exited uncleanly: %v", err) } matched, err := regexp.MatchString(TokenExpectedRegex, stdout) if err != nil { t.Fatalf("encountered an error while trying to match 'kubeadm ex token generate' stdout: %v", err) } if !matched { t.Errorf("'kubeadm ex token generate' stdout did not match expected regex; wanted: [%q], got: [%s]", TokenExpectedRegex, stdout) } } func TestCmdTokenGenerateTypoError(t *testing.T) { /* Since we expect users to do things like this: $ TOKEN=$(kubeadm ex token generate) we want to make sure that if they have a typo in their command, we exit with a non-zero status code after showing the command's usage, so that the usage itself isn't captured as a token without the user noticing. */ _, _, err := RunCmd(kubeadmPath, "ex", "token", "genorate") // subtle typo if err == nil { t.Error("'kubeadm ex token genorate' (a deliberate typo) exited without an error when we expected non-zero exit status") } } </del>

<del> /* Copyright 2016 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 kubeadm import ( "bytes" "fmt" "io" "os" "os/exec" ) // Forked from test/e2e/framework because the e2e framework is quite bloated // for our purposes here, and modified to remove undesired logging. func RunCmd(command string, args ...string) (string, string, error) { var bout, berr bytes.Buffer cmd := exec.Command(command, args...) cmd.Stdout = io.MultiWriter(os.Stdout, &bout) cmd.Stderr = io.MultiWriter(os.Stderr, &berr) err := cmd.Run() stdout, stderr := bout.String(), berr.String() if err != nil { return "", "", fmt.Errorf("error running %s %v; got error %v, stdout %q, stderr %q", command, args, err, stdout, stderr) } return stdout, stderr, nil } </del>

make -C "${KUBE_ROOT}" WHAT=cmd/kubeadm make -C "${KUBE_ROOT}" test  <del> WHAT=k8s.io/kubernetes/cmd/kubeadm/test  </del> <ins> WHAT=k8s.io/kubernetes/cmd/kubeadm/test/cmd  </ins> KUBE_TEST_ARGS="--kubeadm-path '${KUBEADM_PATH}'"

kube-master-url kube-reserved kubeadm-path <ins> kubeadm-cmd-skip </ins> kubecfg-file kubectl-path kubelet-address