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