github.com/qiniu/go-sdk/v7@v7.27.0/cdn/api_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  package cdn
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/qiniu/go-sdk/v7/auth"
    13  )
    14  
    15  // global variables
    16  var (
    17  	ak     = os.Getenv("accessKey")
    18  	sk     = os.Getenv("secretKey")
    19  	domain = os.Getenv("QINIU_TEST_DOMAIN")
    20  
    21  	layout    = "2006-01-02"
    22  	now       = time.Now()
    23  	startDate = now.AddDate(0, 0, -2).Format(layout)
    24  	endDate   = now.AddDate(0, 0, -1).Format(layout)
    25  	logDate   = now.AddDate(0, 0, -1).Format(layout)
    26  
    27  	testUrls = []string{
    28  		fmt.Sprintf("http://%s/qiniu.png", domain),
    29  		fmt.Sprintf("http://%s/qiniu-fetch.png", domain),
    30  	}
    31  	testDirs = []string{
    32  		fmt.Sprintf("http://%s/gosdkintegration/", domain),
    33  		fmt.Sprintf("http://%s/gosdkintegration1/", domain),
    34  	}
    35  )
    36  
    37  var (
    38  	mac        *auth.Credentials
    39  	cdnManager *CdnManager
    40  )
    41  
    42  func init() {
    43  	if ak == "" || sk == "" {
    44  		panic("ak/sk should not be empty")
    45  	}
    46  	mac = auth.New(ak, sk)
    47  	cdnManager = NewCdnManager(mac)
    48  }
    49  
    50  // TestGetBandwidthData
    51  func TestGetBandwidthData(t *testing.T) {
    52  	type args struct {
    53  		startDate   string
    54  		endDate     string
    55  		granularity string
    56  		dataType    DataType
    57  		domainList  []string
    58  	}
    59  
    60  	testCases := []struct {
    61  		name     string
    62  		args     args
    63  		wantCode int
    64  	}{
    65  		{
    66  			name: "CdnManager_TestGetBandwidthData",
    67  			args: args{
    68  				startDate,
    69  				endDate,
    70  				"5min",
    71  				DataTypeBandwidth,
    72  				[]string{domain},
    73  			},
    74  			wantCode: 200,
    75  		},
    76  	}
    77  
    78  	for _, tc := range testCases {
    79  		t.Run(tc.name, func(t *testing.T) {
    80  			ret, err := cdnManager.GetBandwidthData(tc.args.startDate, tc.args.endDate,
    81  				tc.args.granularity, tc.args.domainList, WithBandwidthDataType(tc.args.dataType))
    82  			if err != nil || ret.Code != tc.wantCode {
    83  				t.Errorf("GetBandwidth() error = %v, %v", err, ret.Error)
    84  				return
    85  			}
    86  		})
    87  	}
    88  }
    89  
    90  // TestGetFluxData
    91  func TestGetFluxData(t *testing.T) {
    92  	type args struct {
    93  		startDate   string
    94  		endDate     string
    95  		granularity string
    96  		dataType    DataType
    97  		domainList  []string
    98  	}
    99  
   100  	testCases := []struct {
   101  		name     string
   102  		args     args
   103  		wantCode int
   104  	}{
   105  		{
   106  			name: "CdnManager_TestGetFluxData",
   107  			args: args{
   108  				startDate,
   109  				endDate,
   110  				"5min",
   111  				DataTypeFlow,
   112  				[]string{domain},
   113  			},
   114  			wantCode: 200,
   115  		},
   116  	}
   117  
   118  	for _, tc := range testCases {
   119  		t.Run(tc.name, func(t *testing.T) {
   120  			ret, err := cdnManager.GetFluxData(tc.args.startDate, tc.args.endDate,
   121  				tc.args.granularity, tc.args.domainList, WithFluxDataType(tc.args.dataType))
   122  			if err != nil || ret.Code != tc.wantCode {
   123  				t.Errorf("GetFlux() error = %v, %v", err, ret.Error)
   124  				return
   125  			}
   126  		})
   127  	}
   128  }
   129  
   130  // TestRefreshUrls
   131  func TestRefreshUrls(t *testing.T) {
   132  	type args struct {
   133  		urls []string
   134  	}
   135  
   136  	testCases := []struct {
   137  		name     string
   138  		args     args
   139  		wantCode int
   140  	}{
   141  		{
   142  			name: "CdnManager_TestRefresUrls",
   143  			args: args{
   144  				urls: testUrls,
   145  			},
   146  			wantCode: 200,
   147  		},
   148  	}
   149  
   150  	for _, tc := range testCases {
   151  		t.Run(tc.name, func(t *testing.T) {
   152  			ret, err := cdnManager.RefreshUrls(tc.args.urls)
   153  			if err != nil || ret.Code != tc.wantCode {
   154  				t.Errorf("RefreshUrls() %v error = %v, %v", tc.args.urls, err, ret.Error)
   155  				return
   156  			}
   157  		})
   158  	}
   159  }
   160  
   161  // TestRefreshDirs
   162  func TestRefreshDirs(t *testing.T) {
   163  	type args struct {
   164  		dirs []string
   165  	}
   166  
   167  	testCases := []struct {
   168  		name     string
   169  		args     args
   170  		wantCode int
   171  	}{
   172  		{
   173  			name: "CdnManager_TestRefreshDirs",
   174  			args: args{
   175  				dirs: testDirs,
   176  			},
   177  			wantCode: 200,
   178  		},
   179  	}
   180  
   181  	for _, tc := range testCases {
   182  		t.Run(tc.name, func(t *testing.T) {
   183  			ret, err := cdnManager.RefreshDirs(tc.args.dirs)
   184  			if err != nil || ret.Code != tc.wantCode {
   185  				if ret.Error == "refresh dir limit error" {
   186  					t.Logf("RefreshDirs() error=%v", ret.Error)
   187  				} else {
   188  					t.Errorf("RefreshDirs() error = %v, %v", err, ret.Error)
   189  				}
   190  				return
   191  			}
   192  		})
   193  	}
   194  }
   195  
   196  /* 预取有额度限制
   197     //TestPrefetchUrls
   198     func TestPrefetchUrls(t *testing.T) {
   199  	type args struct {
   200  		urls []string
   201  	}
   202  
   203  	testCases := []struct {
   204  		name     string
   205  		args     args
   206  		wantCode int
   207  	}{
   208  		{
   209  			name: "CdnManager_PrefetchUrls",
   210  			args: args{
   211  				urls: testUrls,
   212  			},
   213  			wantCode: 200,
   214  		},
   215  	}
   216  
   217  	for _, tc := range testCases {
   218  		t.Run(tc.name, func(t *testing.T) {
   219  			ret, err := cdnManager.PrefetchUrls(tc.args.urls)
   220  			if err != nil || ret.Code != tc.wantCode {
   221  				t.Errorf("PrefetchUrls() error = %v, %v", err, ret.Error)
   222  				return
   223  			}
   224  		})
   225  	}
   226          }
   227  */
   228  
   229  // TestGetCdnLogList
   230  func TestGetCdnLogList(t *testing.T) {
   231  	type args struct {
   232  		date    string
   233  		domains []string
   234  	}
   235  
   236  	testCases := []struct {
   237  		name string
   238  		args args
   239  	}{
   240  		{
   241  			name: "CdnManager_TestGetCdnLogList",
   242  			args: args{
   243  				date:    logDate,
   244  				domains: []string{domain},
   245  			},
   246  		},
   247  	}
   248  
   249  	for _, tc := range testCases {
   250  		t.Run(tc.name, func(t *testing.T) {
   251  			_, err := cdnManager.GetCdnLogList(tc.args.date, tc.args.domains)
   252  			if err != nil {
   253  				t.Errorf("GetCdnLogList() error = %v", err)
   254  				return
   255  			}
   256  		})
   257  	}
   258  }