github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/tools/install_test.go (about)

     1  /*
     2  Copyright 2018 Mirantis
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package tools
    18  
    19  import (
    20  	"io/ioutil"
    21  	"os"
    22  	"path/filepath"
    23  	"strings"
    24  	"testing"
    25  )
    26  
    27  func TestInstallCommand(t *testing.T) {
    28  	tmpDir, err := ioutil.TempDir("", "install-test")
    29  	if err != nil {
    30  		t.Fatalf("ioutil.TempDir(): %v", err)
    31  	}
    32  	defer os.RemoveAll(tmpDir)
    33  
    34  	execContent := "foobar"
    35  	execPath := filepath.Join(tmpDir, "topcmd")
    36  	if err := ioutil.WriteFile(execPath, []byte(execContent), 0777); err != nil {
    37  		t.Fatalf("ioutil.WriteFile(): %v", err)
    38  	}
    39  
    40  	cmd := NewInstallCmd(fakeCobraCommand(), execPath, tmpDir)
    41  	cmd.SilenceUsage = true
    42  	cmd.SilenceErrors = true
    43  	if err := cmd.Execute(); err != nil {
    44  		t.Errorf("Error running install command: %v", err)
    45  	}
    46  
    47  	pluginBinaryPath := filepath.Join(tmpDir, ".kube/plugins/virt/virt")
    48  	if bs, err := ioutil.ReadFile(pluginBinaryPath); err != nil {
    49  		t.Errorf("Can't read the plugin file %q: %v", pluginBinaryPath, err)
    50  	} else if string(bs) != execContent {
    51  		t.Errorf("Bad content of the plugin file: %q instead of %q", bs, execContent)
    52  	}
    53  
    54  	pluginYamlPath := filepath.Join(tmpDir, ".kube/plugins/virt/plugin.yaml")
    55  	if bs, err := ioutil.ReadFile(pluginYamlPath); err != nil {
    56  		t.Errorf("Can't read plugin.yaml file %q: %v", pluginYamlPath, err)
    57  	} else if !strings.Contains(string(bs), "Consectetur") || !strings.Contains(string(bs), "./virt") {
    58  		t.Errorf("Bad plugin.yaml content:\n%s", bs)
    59  	}
    60  }