github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/tools/skip.go (about)

     1  // Package tools provides common tools and utilities for all unit and integration tests
     2  /*
     3   * Copyright (c) 2018-2022, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package tools
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  
    11  	"github.com/NVIDIA/aistore/api"
    12  	"github.com/NVIDIA/aistore/cmn"
    13  	"github.com/NVIDIA/aistore/cmn/cos"
    14  	"github.com/NVIDIA/aistore/core/meta"
    15  	"github.com/NVIDIA/aistore/tools/docker"
    16  	"github.com/NVIDIA/aistore/tools/tassert"
    17  )
    18  
    19  type SkipTestArgs struct {
    20  	Bck                   cmn.Bck
    21  	RequiredDeployment    ClusterType
    22  	MinTargets            int
    23  	MinProxies            int
    24  	MinMountpaths         int
    25  	RequiresRemoteCluster bool
    26  	RequiresAuth          bool
    27  	RequiresTLS           bool
    28  	Long                  bool
    29  	RemoteBck             bool
    30  	CloudBck              bool
    31  	RequiredCloudProvider string
    32  	K8s                   bool
    33  	Local                 bool
    34  }
    35  
    36  const fmtSkippingShort = "skipping %s in short mode"
    37  
    38  func ShortSkipf(tb testing.TB, a ...any) {
    39  	var msg string
    40  	if len(a) > 0 {
    41  		msg = fmt.Sprint(a...) + ": "
    42  	}
    43  	msg += fmt.Sprintf(fmtSkippingShort, tb.Name())
    44  	tb.Skip(msg)
    45  }
    46  
    47  func CheckSkip(tb testing.TB, args *SkipTestArgs) {
    48  	var smap *meta.Smap
    49  	if args.RequiresRemoteCluster && RemoteCluster.UUID == "" {
    50  		tb.Skipf("%s requires remote cluster", tb.Name())
    51  	}
    52  	if args.RequiresAuth && LoggedUserToken == "" {
    53  		tb.Skipf("%s requires authentication token", tb.Name())
    54  	}
    55  	if args.RequiresTLS && !cos.IsHTTPS(proxyURLReadOnly) {
    56  		tb.Skipf("%s requires TLS cluster deployment", tb.Name())
    57  	}
    58  	if args.Long && testing.Short() {
    59  		tb.Skipf(fmtSkippingShort, tb.Name())
    60  	}
    61  	if args.RemoteBck {
    62  		proxyURL := GetPrimaryURL()
    63  		if !isRemoteBucket(tb, proxyURL, args.Bck) {
    64  			tb.Skipf("%s requires a remote bucket (have %q)", tb.Name(), args.Bck)
    65  		}
    66  	}
    67  	if args.CloudBck || args.RequiredCloudProvider != "" {
    68  		proxyURL := GetPrimaryURL()
    69  		if !isCloudBucket(tb, proxyURL, args.Bck) {
    70  			tb.Skipf("%s requires a cloud bucket", tb.Name())
    71  		} else if args.RequiredCloudProvider != args.Bck.Provider {
    72  			tb.Skipf("%s requires a cloud bucket with %s provider", tb.Name(), args.RequiredCloudProvider)
    73  		}
    74  	}
    75  
    76  	switch args.RequiredDeployment {
    77  	case ClusterTypeK8s:
    78  		// NOTE: The test suite doesn't have to be deployed on K8s, the cluster has to be.
    79  		isK8s, err := isClusterK8s()
    80  		if err != nil {
    81  			tb.Fatalf("Unrecognized error upon checking K8s deployment; err: %v", err)
    82  		}
    83  		if !isK8s {
    84  			tb.Skipf("%s requires Kubernetes", tb.Name())
    85  		}
    86  	case ClusterTypeLocal:
    87  		isLocal, err := isClusterLocal()
    88  		tassert.CheckFatal(tb, err)
    89  		if !isLocal {
    90  			tb.Skipf("%s requires local deployment", tb.Name())
    91  		}
    92  	case ClusterTypeDocker:
    93  		if !docker.IsRunning() {
    94  			tb.Skipf("%s requires docker deployment", tb.Name())
    95  		}
    96  	}
    97  
    98  	if args.MinTargets > 0 || args.MinMountpaths > 0 || args.MinProxies > 0 {
    99  		smap = GetClusterMap(tb, GetPrimaryURL())
   100  	}
   101  
   102  	if args.MinTargets > 0 {
   103  		if smap.CountTargets() < args.MinTargets {
   104  			tb.Skipf("%s requires at least %d targets (have %d)",
   105  				tb.Name(), args.MinTargets, smap.CountTargets())
   106  		}
   107  	}
   108  
   109  	if args.MinProxies > 0 {
   110  		if smap.CountProxies() < args.MinProxies {
   111  			tb.Skipf("%s requires at least %d proxies (have %d)",
   112  				tb.Name(), args.MinProxies, smap.CountProxies())
   113  		}
   114  	}
   115  
   116  	if args.MinMountpaths > 0 {
   117  		targets := smap.Tmap.ActiveNodes()
   118  		proxyURL := GetPrimaryURL()
   119  		bp := BaseAPIParams(proxyURL)
   120  		mpList, err := api.GetMountpaths(bp, targets[0])
   121  		tassert.CheckFatal(tb, err)
   122  		if l := len(mpList.Available); l < args.MinMountpaths {
   123  			tb.Skipf("%s requires at least %d mountpaths (have %d)", tb.Name(), args.MinMountpaths, l)
   124  		}
   125  	}
   126  }