go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/core/resources/time_test.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package resources_test
     5  
     6  import (
     7  	"math"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"go.mondoo.com/cnquery/llx"
    13  	"go.mondoo.com/cnquery/providers-sdk/v1/testutils"
    14  )
    15  
    16  func duration(i int64) *time.Time {
    17  	res := llx.DurationToTime(i)
    18  	return &res
    19  }
    20  
    21  func TestFuzzyTime(t *testing.T) {
    22  	x := testutils.InitTester(testutils.LinuxMock())
    23  	code := "time.now.unix"
    24  	t.Run(code, func(t *testing.T) {
    25  		res := x.TestQuery(t, code)
    26  		now := time.Now().Unix()
    27  		assert.NotEmpty(t, res)
    28  
    29  		assert.NotNil(t, res[0].Result().Error)
    30  		val := res[0].Data.Value
    31  		valInt, ok := val.(int64)
    32  		assert.Equal(t, true, ok)
    33  		min := now - 1
    34  		max := now + 1
    35  		between := min <= valInt && valInt <= max
    36  		assert.Equal(t, true, between)
    37  	})
    38  }
    39  
    40  func TestTimeParsing(t *testing.T) {
    41  	parserTimestamp := int64(1136214245)
    42  
    43  	x := testutils.InitTester(testutils.LinuxMock())
    44  	x.TestSimple(t, []testutils.SimpleTest{
    45  		{
    46  			Code:        "parse.date('0000-01-01T02:03:04Z').seconds",
    47  			Expectation: int64(4 + 3*60 + 2*60*60),
    48  		},
    49  		{
    50  			Code:        "parse.date('0000-01-01T02:03:04Z').minutes",
    51  			Expectation: int64(3 + 2*60),
    52  		},
    53  		{
    54  			Code:        "parse.date('0000-01-01T02:03:04Z').hours",
    55  			Expectation: int64(2),
    56  		},
    57  		{
    58  			Code:        "parse.date('0000-01-11T02:03:04Z').days",
    59  			Expectation: int64(10),
    60  		},
    61  		{
    62  			Code:        "parse.date('1970-01-01T01:02:03Z').unix",
    63  			Expectation: int64(1*60*60 + 0o2*60 + 0o3),
    64  		},
    65  		{
    66  			Code:        "parse.date('1970-01-01T01:02:04Z') - parse.date('1970-01-01T01:02:03Z')",
    67  			Expectation: duration(1),
    68  		},
    69  		{
    70  			Code:        "parse.date('0000-01-01T00:00:03Z') * 3",
    71  			Expectation: duration(9),
    72  		},
    73  		// Testing all the default parsers
    74  		{
    75  			Code:        "parse.date('2006-01-02T15:04:05Z').unix",
    76  			Expectation: parserTimestamp,
    77  		},
    78  		{
    79  			Code:        "parse.date('2006-01-02 15:04:05').unix",
    80  			Expectation: parserTimestamp,
    81  		},
    82  		{
    83  			Code:        "parse.date('2006-01-02').unix",
    84  			Expectation: parserTimestamp - (15*60*60 + 4*60 + 5),
    85  		},
    86  		{
    87  			Code:        "parse.date('15:04:05').unix",
    88  			Expectation: duration(15*60*60 + 4*60 + 5).Unix(),
    89  		},
    90  		{
    91  			Code:        "parse.date('Mon, 02 Jan 2006 15:04:05 MST').unix",
    92  			Expectation: parserTimestamp,
    93  		},
    94  		{
    95  			Code:        "parse.date('Mon Jan 2 15:04:05 2006').unix",
    96  			Expectation: parserTimestamp,
    97  		},
    98  		{
    99  			Code:        "parse.date('02 Jan 06 15:04 MST').unix",
   100  			Expectation: parserTimestamp - 5, // since it doesn't have seconds
   101  		},
   102  		{
   103  			Code:        "parse.date('Monday, 02-Jan-06 15:04:05 MST').unix",
   104  			Expectation: parserTimestamp,
   105  		},
   106  		{
   107  			Code:        "parse.date('3:04PM').unix",
   108  			Expectation: duration(15*60*60 + 4*60).Unix(),
   109  		},
   110  		{
   111  			Code:        "parse.date('Jan 2 15:04:05').unix",
   112  			Expectation: duration(1*24*60*60 + 15*60*60 + 4*60 + 5).Unix(),
   113  		},
   114  	})
   115  
   116  	parserTimestampTZ := int64(1136239445)
   117  	x.TestSimple(t, []testutils.SimpleTest{
   118  		{
   119  			Code:        "parse.date('Mon, 02 Jan 2006 15:04:05 -0700').unix",
   120  			Expectation: parserTimestampTZ,
   121  		},
   122  		{
   123  			Code:        "parse.date('02 Jan 06 15:04 -0700').unix",
   124  			Expectation: parserTimestampTZ - 5, // since it doesn't have seconds
   125  		},
   126  	})
   127  }
   128  
   129  func TestTime_Methods(t *testing.T) {
   130  	now := time.Now()
   131  	today, _ := time.ParseInLocation("2006-01-02", now.Format("2006-01-02"), now.Location())
   132  	tomorrow := today.Add(24 * time.Hour)
   133  
   134  	x := testutils.InitTester(testutils.LinuxMock())
   135  	x.TestSimple(t, []testutils.SimpleTest{
   136  		{
   137  			Code:        "time.now > time.today",
   138  			ResultIndex: 2,
   139  			Expectation: true,
   140  		},
   141  		{
   142  			Code:        "time.today",
   143  			Expectation: &today,
   144  		},
   145  		{
   146  			Code:        "time.tomorrow",
   147  			Expectation: &tomorrow,
   148  		},
   149  		{
   150  			Code:        "time.hour",
   151  			Expectation: duration(60 * 60),
   152  		},
   153  		{
   154  			Code:        "2*time.hour + 1*time.hour",
   155  			Expectation: duration(3 * 60 * 60),
   156  		},
   157  		{
   158  			Code:        "time.today + 1*time.day",
   159  			Expectation: &tomorrow,
   160  		},
   161  		{
   162  			Code:        "2*time.hour - 1*time.hour",
   163  			Expectation: duration(60 * 60),
   164  		},
   165  		{
   166  			Code:        "3 * time.second",
   167  			Expectation: duration(3),
   168  		},
   169  		{
   170  			Code:        "3 * time.minute",
   171  			Expectation: duration(3 * 60),
   172  		},
   173  		{
   174  			Code:        "3 * time.hour",
   175  			Expectation: duration(3 * 60 * 60),
   176  		},
   177  		{
   178  			Code:        "3 * time.day",
   179  			Expectation: duration(3 * 60 * 60 * 24),
   180  		},
   181  		{
   182  			Code:        "1 * time.day > 3 * time.hour",
   183  			ResultIndex: 2, Expectation: true,
   184  		},
   185  		{
   186  			Code:        "time.now != Never",
   187  			ResultIndex: 2, Expectation: true,
   188  		},
   189  		{
   190  			Code:        "time.now - Never",
   191  			Expectation: &llx.NeverPastTime,
   192  		},
   193  		{
   194  			Code:        "Never - time.now",
   195  			Expectation: &llx.NeverFutureTime,
   196  		},
   197  		{
   198  			Code:        "Never - Never",
   199  			Expectation: &llx.NeverPastTime,
   200  		},
   201  		{
   202  			Code:        "Never * 3",
   203  			Expectation: &llx.NeverFutureTime,
   204  		},
   205  		{
   206  			Code:        "a = Never - time.now; a.days",
   207  			Expectation: int64(math.MaxInt64),
   208  		},
   209  	})
   210  }