github.com/sacloud/iaas-api-go@v1.12.0/naked/monitor_test.go (about)

     1  // Copyright 2022-2023 The sacloud/iaas-api-go Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package naked
    16  
    17  import (
    18  	"encoding/json"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  const (
    26  	testMonitorValuesCPUTimeJSON = `
    27      {
    28          "1970-01-01T00:00:01Z": {
    29              "CPU-TIME": 0.5
    30          },
    31          "1970-01-01T00:00:02Z": {
    32              "CPU-TIME": 0.1
    33          },
    34          "1970-01-01T00:00:03Z": {
    35              "CPU-TIME": 0
    36          },
    37          "1970-01-01T00:00:04Z": {
    38              "CPU-TIME": null
    39          }
    40      }`
    41  
    42  	testMonitorValuesDiskJSON = `
    43      {
    44          "1970-01-01T00:00:01Z": {
    45              "Read": 10.0, 
    46              "Write": 20.0 
    47          },
    48          "1970-01-01T00:00:02Z": {
    49              "Read": 11.0, 
    50              "Write": 21.0 
    51          },
    52          "1970-01-01T00:00:03Z": {
    53              "Read": null, 
    54              "Write": null 
    55          },
    56          "1970-01-01T00:00:04Z": {
    57              "Read": 12.0, 
    58              "Write": 22.0 
    59          }
    60      }`
    61  
    62  	testMonitorValuesInterfaceJSON = `
    63      {
    64          "1970-01-01T00:00:01Z": {
    65              "Send": 10.0, 
    66              "Receive": 20.0 
    67          },
    68          "1970-01-01T00:00:02Z": {
    69              "Send": 11.0, 
    70              "Receive": 21.0 
    71          },
    72          "1970-01-01T00:00:03Z": {
    73              "Send": null, 
    74              "Receive": null 
    75          },
    76          "1970-01-01T00:00:04Z": {
    77              "Send": 12.0, 
    78              "Receive": 22.0 
    79          }
    80      }`
    81  
    82  	testMonitorValuesRouterJSON = `
    83      {
    84          "1970-01-01T00:00:01Z": {
    85              "In": 10.0, 
    86              "Out": 20.0 
    87          },
    88          "1970-01-01T00:00:02Z": {
    89              "In": 11.0, 
    90              "Out": 21.0 
    91          },
    92          "1970-01-01T00:00:03Z": {
    93              "In": null, 
    94              "Out": null 
    95          },
    96          "1970-01-01T00:00:04Z": {
    97              "In": 12.0, 
    98              "Out": 22.0 
    99          }
   100      }`
   101  
   102  	testMonitorValuesDatabaseJSON = `
   103      {
   104          "1970-01-01T00:00:01Z": {
   105              "Total-Memory-Size"  : 10.0, 
   106              "Used-Memory-Size"   : 20.0,
   107              "Total-Disk1-Size"   : 30.0,
   108              "Used-Disk1-Size"    : 40.0,
   109              "Total-Disk2-Size"   : 50.0,
   110              "Used-Disk2-Size"    : 60.0,
   111              "binlogUsedSizeKiB": 70.0,
   112              "delayTimeSec"     : 80.0
   113          },
   114          "1970-01-01T00:00:02Z": {
   115              "Total-Memory-Size"  : 11.0, 
   116              "Used-Memory-Size"   : 21.0,
   117              "Total-Disk1-Size"   : 31.0,
   118              "Used-Disk1-Size"    : 41.0,
   119              "Total-Disk2-Size"   : 51.0,
   120              "Used-Disk2-Size"    : 61.0,
   121              "binlogUsedSizeKiB": 71.0,
   122              "delayTimeSec"     : 81.0
   123          },
   124          "1970-01-01T00:00:03Z": {
   125              "Total-Memory-Size"  : null, 
   126              "Used-Memory-Size"   : null,
   127              "Total-Disk1-Size"   : null,
   128              "Used-Disk1-Size"    : null,
   129              "Total-Disk2-Size"   : null,
   130              "Used-Disk2-Size"    : null,
   131              "binlogUsedSizeKiB": null,
   132              "delayTimeSec"     : null 
   133          },
   134          "1970-01-01T00:00:04Z": {
   135              "Total-Memory-Size"  : 12.0, 
   136              "Used-Memory-Size"   : 22.0,
   137              "Total-Disk1-Size"   : 32.0,
   138              "Used-Disk1-Size"    : 42.0,
   139              "Total-Disk2-Size"   : 52.0,
   140              "Used-Disk2-Size"    : 62.0,
   141              "binlogUsedSizeKiB": 72.0,
   142              "delayTimeSec"     : 82.0
   143          }
   144      }`
   145  
   146  	testMonitorValuesFreeDiskSizeJSON = `
   147      {
   148          "1970-01-01T00:00:01Z": {
   149              "Free-Disk-Size": 0.5
   150          },
   151          "1970-01-01T00:00:02Z": {
   152              "Free-Disk-Size": 0.1
   153          },
   154          "1970-01-01T00:00:03Z": {
   155              "Free-Disk-Size": 0
   156          },
   157          "1970-01-01T00:00:04Z": {
   158              "Free-Disk-Size": null
   159          }
   160      }`
   161  
   162  	testMonitorValuesResponseTimeSecJSON = `
   163      {
   164          "1970-01-01T00:00:01Z": {
   165              "responsetimesec": 0.5
   166          },
   167          "1970-01-01T00:00:02Z": {
   168              "responsetimesec": 0.1
   169          },
   170          "1970-01-01T00:00:03Z": {
   171              "responsetimesec": 0
   172          },
   173          "1970-01-01T00:00:04Z": {
   174              "responsetimesec": null
   175          }
   176      }`
   177  
   178  	testMonitorValuesLinkJSON = `
   179      {
   180          "1970-01-01T00:00:01Z": {
   181              "UplinkBps": 10.0, 
   182              "DownlinkBps": 20.0 
   183          },
   184          "1970-01-01T00:00:02Z": {
   185              "UplinkBps": 11.0, 
   186              "DownlinkBps": 21.0 
   187          },
   188          "1970-01-01T00:00:03Z": {
   189              "UplinkBps": null, 
   190              "DownlinkBps": null 
   191          },
   192          "1970-01-01T00:00:04Z": {
   193              "UplinkBps": 12.0, 
   194              "DownlinkBps": 22.0 
   195          }
   196      }`
   197  
   198  	testMonitorValuesConnexctionJSON = `
   199      {
   200          "1970-01-01T00:00:01Z": {
   201              "activeConnections": 10.0, 
   202              "connectionsPerSec": 20.0 
   203          },
   204          "1970-01-01T00:00:02Z": {
   205              "activeConnections": 11.0, 
   206              "connectionsPerSec": 21.0 
   207          },
   208          "1970-01-01T00:00:03Z": {
   209              "activeConnections": null, 
   210              "connectionsPerSec": null 
   211          },
   212          "1970-01-01T00:00:04Z": {
   213              "activeConnections": 12.0, 
   214              "connectionsPerSec": 22.0 
   215          }
   216      }`
   217  
   218  	testMonitorValuesLocalRouterJSON = `
   219      {
   220          "1970-01-01T00:00:01Z": {
   221              "receiveBytesPerSec": 10.0, 
   222              "sendBytesPerSec": 20.0 
   223          },
   224          "1970-01-01T00:00:02Z": {
   225              "receiveBytesPerSec": 11.0, 
   226              "sendBytesPerSec": 21.0 
   227          },
   228          "1970-01-01T00:00:03Z": {
   229              "receiveBytesPerSec": null, 
   230              "sendBytesPerSec": null 
   231          },
   232          "1970-01-01T00:00:04Z": {
   233              "receiveBytesPerSec": 12.0, 
   234              "sendBytesPerSec": 22.0 
   235          }
   236      }`
   237  )
   238  
   239  func TestMonitorValues_UnmarshalJSON(t *testing.T) {
   240  	expects := []struct {
   241  		input  string
   242  		expect MonitorValues
   243  	}{
   244  		{
   245  			input: testMonitorValuesCPUTimeJSON,
   246  			expect: MonitorValues{
   247  				CPU: MonitorCPUTimeValues{
   248  					{
   249  						Time:    time.Unix(1, 0).UTC(),
   250  						CPUTime: float64(0.5),
   251  					},
   252  					{
   253  						Time:    time.Unix(2, 0).UTC(),
   254  						CPUTime: float64(0.1),
   255  					},
   256  					{
   257  						Time:    time.Unix(3, 0).UTC(),
   258  						CPUTime: float64(0),
   259  					},
   260  				},
   261  			},
   262  		},
   263  		{
   264  			input: testMonitorValuesDiskJSON,
   265  			expect: MonitorValues{
   266  				Disk: MonitorDiskValues{
   267  					{
   268  						Time:  time.Unix(1, 0).UTC(),
   269  						Read:  float64(10.0),
   270  						Write: float64(20.0),
   271  					},
   272  					{
   273  						Time:  time.Unix(2, 0).UTC(),
   274  						Read:  float64(11.0),
   275  						Write: float64(21.0),
   276  					},
   277  					{
   278  						Time:  time.Unix(4, 0).UTC(),
   279  						Read:  float64(12.0),
   280  						Write: float64(22.0),
   281  					},
   282  				},
   283  			},
   284  		},
   285  		{
   286  			input: testMonitorValuesInterfaceJSON,
   287  			expect: MonitorValues{
   288  				Interface: MonitorInterfaceValues{
   289  					{
   290  						Time:    time.Unix(1, 0).UTC(),
   291  						Send:    float64(10.0),
   292  						Receive: float64(20.0),
   293  					},
   294  					{
   295  						Time:    time.Unix(2, 0).UTC(),
   296  						Send:    float64(11.0),
   297  						Receive: float64(21.0),
   298  					},
   299  					{
   300  						Time:    time.Unix(4, 0).UTC(),
   301  						Send:    float64(12.0),
   302  						Receive: float64(22.0),
   303  					},
   304  				},
   305  			},
   306  		},
   307  		{
   308  			input: testMonitorValuesRouterJSON,
   309  			expect: MonitorValues{
   310  				Router: MonitorRouterValues{
   311  					{
   312  						Time: time.Unix(1, 0).UTC(),
   313  						In:   float64(10.0),
   314  						Out:  float64(20.0),
   315  					},
   316  					{
   317  						Time: time.Unix(2, 0).UTC(),
   318  						In:   float64(11.0),
   319  						Out:  float64(21.0),
   320  					},
   321  					{
   322  						Time: time.Unix(4, 0).UTC(),
   323  						In:   float64(12.0),
   324  						Out:  float64(22.0),
   325  					},
   326  				},
   327  			},
   328  		},
   329  		{
   330  			input: testMonitorValuesDatabaseJSON,
   331  			expect: MonitorValues{
   332  				Database: MonitorDatabaseValues{
   333  					{
   334  						Time:              time.Unix(1, 0).UTC(),
   335  						TotalMemorySize:   float64(10.0),
   336  						UsedMemorySize:    float64(20.0),
   337  						TotalDisk1Size:    float64(30.0),
   338  						UsedDisk1Size:     float64(40.0),
   339  						TotalDisk2Size:    float64(50.0),
   340  						UsedDisk2Size:     float64(60.0),
   341  						BinlogUsedSizeKiB: float64(70.0),
   342  						DelayTimeSec:      float64(80.0),
   343  					},
   344  					{
   345  						Time:              time.Unix(2, 0).UTC(),
   346  						TotalMemorySize:   float64(11.0),
   347  						UsedMemorySize:    float64(21.0),
   348  						TotalDisk1Size:    float64(31.0),
   349  						UsedDisk1Size:     float64(41.0),
   350  						TotalDisk2Size:    float64(51.0),
   351  						UsedDisk2Size:     float64(61.0),
   352  						BinlogUsedSizeKiB: float64(71.0),
   353  						DelayTimeSec:      float64(81.0),
   354  					},
   355  					{
   356  						Time:              time.Unix(4, 0).UTC(),
   357  						TotalMemorySize:   float64(12.0),
   358  						UsedMemorySize:    float64(22.0),
   359  						TotalDisk1Size:    float64(32.0),
   360  						UsedDisk1Size:     float64(42.0),
   361  						TotalDisk2Size:    float64(52.0),
   362  						UsedDisk2Size:     float64(62.0),
   363  						BinlogUsedSizeKiB: float64(72.0),
   364  						DelayTimeSec:      float64(82.0),
   365  					},
   366  				},
   367  			},
   368  		},
   369  		{
   370  			input: testMonitorValuesFreeDiskSizeJSON,
   371  			expect: MonitorValues{
   372  				FreeDiskSize: MonitorFreeDiskSizeValues{
   373  					{
   374  						Time:         time.Unix(1, 0).UTC(),
   375  						FreeDiskSize: float64(0.5),
   376  					},
   377  					{
   378  						Time:         time.Unix(2, 0).UTC(),
   379  						FreeDiskSize: float64(0.1),
   380  					},
   381  					{
   382  						Time:         time.Unix(3, 0).UTC(),
   383  						FreeDiskSize: float64(0),
   384  					},
   385  				},
   386  			},
   387  		},
   388  		{
   389  			input: testMonitorValuesResponseTimeSecJSON,
   390  			expect: MonitorValues{
   391  				ResponseTimeSec: MonitorResponseTimeSecValues{
   392  					{
   393  						Time:            time.Unix(1, 0).UTC(),
   394  						ResponseTimeSec: float64(0.5),
   395  					},
   396  					{
   397  						Time:            time.Unix(2, 0).UTC(),
   398  						ResponseTimeSec: float64(0.1),
   399  					},
   400  					{
   401  						Time:            time.Unix(3, 0).UTC(),
   402  						ResponseTimeSec: float64(0),
   403  					},
   404  				},
   405  			},
   406  		},
   407  		{
   408  			input: testMonitorValuesLinkJSON,
   409  			expect: MonitorValues{
   410  				Link: MonitorLinkValues{
   411  					{
   412  						Time:        time.Unix(1, 0).UTC(),
   413  						UplinkBPS:   float64(10.0),
   414  						DownlinkBPS: float64(20.0),
   415  					},
   416  					{
   417  						Time:        time.Unix(2, 0).UTC(),
   418  						UplinkBPS:   float64(11.0),
   419  						DownlinkBPS: float64(21.0),
   420  					},
   421  					{
   422  						Time:        time.Unix(4, 0).UTC(),
   423  						UplinkBPS:   float64(12.0),
   424  						DownlinkBPS: float64(22.0),
   425  					},
   426  				},
   427  			},
   428  		},
   429  		{
   430  			input: testMonitorValuesConnexctionJSON,
   431  			expect: MonitorValues{
   432  				Connection: MonitorConnectionValues{
   433  					{
   434  						Time:              time.Unix(1, 0).UTC(),
   435  						ActiveConnections: float64(10.0),
   436  						ConnectionsPerSec: float64(20.0),
   437  					},
   438  					{
   439  						Time:              time.Unix(2, 0).UTC(),
   440  						ActiveConnections: float64(11.0),
   441  						ConnectionsPerSec: float64(21.0),
   442  					},
   443  					{
   444  						Time:              time.Unix(4, 0).UTC(),
   445  						ActiveConnections: float64(12.0),
   446  						ConnectionsPerSec: float64(22.0),
   447  					},
   448  				},
   449  			},
   450  		},
   451  		{
   452  			input: testMonitorValuesLocalRouterJSON,
   453  			expect: MonitorValues{
   454  				LocalRouter: MonitorLocalRouterValues{
   455  					{
   456  						Time:               time.Unix(1, 0).UTC(),
   457  						ReceiveBytesPerSec: float64(10.0),
   458  						SendBytesPerSec:    float64(20.0),
   459  					},
   460  					{
   461  						Time:               time.Unix(2, 0).UTC(),
   462  						ReceiveBytesPerSec: float64(11.0),
   463  						SendBytesPerSec:    float64(21.0),
   464  					},
   465  					{
   466  						Time:               time.Unix(4, 0).UTC(),
   467  						ReceiveBytesPerSec: float64(12.0),
   468  						SendBytesPerSec:    float64(22.0),
   469  					},
   470  				},
   471  			},
   472  		},
   473  	}
   474  
   475  	for _, tc := range expects {
   476  		dest := MonitorValues{}
   477  		err := json.Unmarshal([]byte(tc.input), &dest)
   478  		require.NoError(t, err)
   479  		require.Equal(t, tc.expect, dest)
   480  	}
   481  }