github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/tests/longevity/runner.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 longevity
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"os"
    23  	"os/signal"
    24  	"syscall"
    25  	"time"
    26  
    27  	"github.com/Mirantis/virtlet/tests/e2e/framework"
    28  	"github.com/golang/glog"
    29  )
    30  
    31  func GetBaseTests(controller *framework.Controller) []*VMInstance {
    32  	return []*VMInstance{
    33  		{
    34  			name:         "cirros-base-test",
    35  			controller:   controller,
    36  			lifetime:     time.Duration(24) * time.Hour,
    37  			testWaitTime: time.Duration(1) * time.Hour,
    38  		},
    39  	}
    40  }
    41  
    42  func GetStressTests(controller *framework.Controller) []*VMInstance {
    43  	return []*VMInstance{
    44  		{
    45  			name:         "cirros-stress-1min",
    46  			controller:   controller,
    47  			lifetime:     time.Duration(1) * time.Minute,
    48  			testWaitTime: time.Duration(1) * time.Minute,
    49  		},
    50  		{
    51  			name:         "cirros-stress-3min",
    52  			controller:   controller,
    53  			lifetime:     time.Duration(3) * time.Minute,
    54  			testWaitTime: time.Duration(1) * time.Minute,
    55  		},
    56  		{
    57  			name:         "cirros-stress-5min",
    58  			controller:   controller,
    59  			lifetime:     time.Duration(5) * time.Minute,
    60  			testWaitTime: time.Duration(1) * time.Minute,
    61  		},
    62  		{
    63  			name:         "cirros-stress-10min",
    64  			controller:   controller,
    65  			lifetime:     time.Duration(10) * time.Minute,
    66  			testWaitTime: time.Duration(1) * time.Minute,
    67  		},
    68  		{
    69  			name:         "cirros-stress-15min",
    70  			controller:   controller,
    71  			lifetime:     time.Duration(15) * time.Minute,
    72  			testWaitTime: time.Duration(1) * time.Minute,
    73  		},
    74  	}
    75  }
    76  
    77  func Run(controller *framework.Controller, instances []*VMInstance) error {
    78  	var err error
    79  	errChan := make(chan error)
    80  
    81  	exitChan := make(chan os.Signal, 1)
    82  	signal.Notify(exitChan, os.Interrupt, syscall.SIGTERM)
    83  	ctx, cancel := context.WithCancel(context.Background())
    84  	go func() {
    85  		select {
    86  		case <-exitChan:
    87  			glog.V(4).Infof("CTRL-C received")
    88  			cancel()
    89  		case <-ctx.Done():
    90  			break
    91  		}
    92  	}()
    93  
    94  	_, err = startNginxPod(controller)
    95  	if err != nil {
    96  		return fmt.Errorf("Couldn't start  nginx pod: %v", err)
    97  	}
    98  
    99  	for _, instance := range instances {
   100  		glog.Infof("Creating `%s` VM...", instance.name)
   101  		err = instance.Create()
   102  		if err != nil {
   103  			return fmt.Errorf("Could not create VM: %v", err)
   104  		}
   105  		glog.V(4).Infof("Done")
   106  	}
   107  	for _, instance := range instances {
   108  		go instance.Test(ctx, instance, testVM, errChan)
   109  	}
   110  
   111  MainLoop:
   112  	for {
   113  		select {
   114  		case err = <-errChan:
   115  			glog.V(4).Infof("Received error: %v", err)
   116  			cancel()
   117  			return err
   118  		case <-ctx.Done():
   119  			glog.Infof("Finishing testing...")
   120  			break MainLoop
   121  		}
   122  	}
   123  	return nil
   124  }