github.com/maxgio92/test-infra@v0.1.0/kubetest/conformance/conformance.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     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 conformance implements conformance test kubetest code.
    18  package conformance
    19  
    20  import (
    21  	"context"
    22  	"fmt"
    23  	"log"
    24  	"os"
    25  	"os/exec"
    26  	"time"
    27  
    28  	v1 "k8s.io/api/core/v1"
    29  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    30  	"k8s.io/client-go/kubernetes"
    31  	"k8s.io/client-go/tools/clientcmd"
    32  	"github.com/maxgio92/test-infra/kubetest/e2e"
    33  )
    34  
    35  // BuildTester returns an object that knows how to test the cluster it deployed.
    36  func (d *Deployer) BuildTester(o *e2e.BuildTesterOptions) (e2e.Tester, error) {
    37  	reportdir, err := os.Getwd()
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  
    42  	if o.FocusRegex == "" {
    43  		o.FocusRegex = "\".*\""
    44  	}
    45  	if o.SkipRegex == "" {
    46  		o.SkipRegex = "\".*(Feature)|(NFS)|(StatefulSet).*\""
    47  	}
    48  
    49  	t := e2e.NewGinkgoTester(o)
    50  
    51  	t.Seed = 1436380640
    52  	t.GinkgoParallel = 10
    53  	t.Kubeconfig = d.kubecfg
    54  	t.FlakeAttempts = 1
    55  	t.NumNodes = 4
    56  	t.SystemdServices = []string{"docker", "kubelet"}
    57  	t.ReportDir = reportdir
    58  
    59  	return t, nil
    60  }
    61  
    62  // Deployer returns a deployer stub that expects a cluster to already exist.
    63  type Deployer struct {
    64  	kubecfg   string
    65  	apiserver *kubernetes.Clientset
    66  }
    67  
    68  // Deployer implements e2e.TestBuilder, overriding testing
    69  var _ e2e.TestBuilder = &Deployer{}
    70  
    71  // NewDeployer returns a new Deployer.
    72  func NewDeployer(kubecfg string) (*Deployer, error) {
    73  	// The easiest thing to do is just load the altereted kubecfg from the file we wrote.
    74  	config, err := clientcmd.BuildConfigFromFlags("", kubecfg)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  	apiserver, err := kubernetes.NewForConfig(config)
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	return &Deployer{
    84  		kubecfg:   kubecfg,
    85  		apiserver: apiserver,
    86  	}, nil
    87  }
    88  
    89  // Up synchronously starts a cluster, or times out.
    90  func (d *Deployer) Up() error {
    91  	return fmt.Errorf("cannot up a conformance cluster")
    92  }
    93  
    94  // IsUp returns nil if the apiserver is running, or the error received while checking.
    95  func (d *Deployer) IsUp() error {
    96  	_, err := d.isAPIServerUp()
    97  	return err
    98  }
    99  
   100  func (d *Deployer) isAPIServerUp() (*v1.ComponentStatusList, error) {
   101  	if d.apiserver == nil {
   102  		return nil, fmt.Errorf("no apiserver client available")
   103  	}
   104  	//TODO(Q-Lee): check that relevant components have started. May consider checking addons.
   105  	return d.apiserver.CoreV1().ComponentStatuses().List(context.TODO(), metav1.ListOptions{})
   106  }
   107  
   108  // DumpClusterLogs is a no-op.
   109  func (d *Deployer) DumpClusterLogs(localPath, gcsPath string) error {
   110  	return nil
   111  }
   112  
   113  // TestSetup is a no-op.
   114  func (d *Deployer) TestSetup() error {
   115  	return nil
   116  }
   117  
   118  // Down stops and removes the cluster container.
   119  func (d *Deployer) Down() error {
   120  	return fmt.Errorf("cannot down a conformance cluster")
   121  }
   122  
   123  // GetClusterCreated returns the start time of the cluster container. If the container doesn't exist, has no start time, or has a malformed start time, then an error is returned.
   124  func (d *Deployer) GetClusterCreated(gcpProject string) (time.Time, error) {
   125  	return time.Time{}, fmt.Errorf("cannot get cluster create time for conformance cluster")
   126  }
   127  
   128  func (d *Deployer) KubectlCommand() (*exec.Cmd, error) {
   129  	log.Print("Noop - Conformance KubectlCommand()")
   130  	return nil, nil
   131  }