func TestGetStartTime(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping in short mode")
	}
	testCases := []struct {
		unit      string
		err       error
		opts      Options
		runAsUser bool
	}{
		// Run these tests only as a user
		//try nonexistant unit in user mode as user
		{"nonexistant", ErrUnitNotActive, Options{UserMode: false}, true},
		// try existing unit in user mode as user
		{"syncthing", ErrUnitNotActive, Options{UserMode: true}, true},
		// try existing unit in system mode as user
		{"nginx", nil, Options{UserMode: false}, true},

		// Run these tests only as a superuser

		// try nonexistant unit in system mode as system
		{"nonexistant", ErrUnitNotActive, Options{UserMode: false}, false},
		// try existing unit in system mode as system
		{"nginx", ErrBusFailure, Options{UserMode: true}, false},
		// try existing unit in system mode as system
		{"nginx", nil, Options{UserMode: false}, false},
	}
	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
	defer cancel()
	Restart(ctx, "syncthing", Options{UserMode: true})
	Stop(ctx, "syncthing", Options{UserMode: true})
	time.Sleep(1 * time.Second)
	for _, tc := range testCases {
		t.Run(fmt.Sprintf("%s as %s, UserMode=%v", tc.unit, userString, tc.opts.UserMode), func(t *testing.T) {
			if (userString == "root" || userString == "system") && tc.runAsUser {
				t.Skip("skipping user test while running as superuser")
			} else if (userString != "root" && userString != "system") && !tc.runAsUser {
				t.Skip("skipping superuser test while running as user")
			}
			ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
			defer cancel()
			_, err := GetStartTime(ctx, tc.unit, tc.opts)
			if err != tc.err {
				t.Errorf("error is %v, but should have been %v", err, tc.err)
			}
		})
	}
	// Prove start time changes after a restart
	t.Run(fmt.Sprintf("prove start time changes"), func(t *testing.T) {
		if userString != "root" && userString != "system" {
			t.Skip("skipping superuser test while running as user")
		}

		ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
		defer cancel()
		startTime, err := GetStartTime(ctx, "nginx", Options{UserMode: false})
		if err != nil {
			t.Errorf("issue getting start time of nginx: %v", err)
		}
		time.Sleep(1 * time.Second)
		err = Restart(ctx, "nginx", Options{UserMode: false})
		if err != nil {
			t.Errorf("issue restarting nginx as %s: %v", userString, err)
		}
		time.Sleep(100 * time.Millisecond)
		newStartTime, err := GetStartTime(ctx, "nginx", Options{UserMode: false})
		if err != nil {
			t.Errorf("issue getting second start time of nginx: %v", err)
		}
		diff := newStartTime.Sub(startTime).Seconds()
		if diff <= 0 {
			t.Errorf("Expected start diff to be positive, but got: %d", int(diff))
		}
	})

}
// Testing assumptions
// - there's no unit installed named `nonexistant`
// - the syncthing unit is available but the binary is not on the tester's system
//   this is just what was available on mine, should you want to change it,
//   either to something in this repo or more common, feel free to submit a PR.
// - your 'user' isn't root
// - your user doesn't have a PolKit rule allowing access to configure nginx
Testing assumptions
- there's no unit installed named `nonexistant`
- the syncthing unit is available but the binary is not on the tester's system
this is just what was available on mine, should you want to change it,
either to something in this repo or more common, feel free to submit a PR.
- your 'user' isn't root
- your user doesn't have a PolKit rule allowing access to configure nginx
// Testing assumptions // - there ' s no unit installed named ` nonexistant ` // - the syncthing unit is available but the binary is not on the tester ' s system // this is just what was available on mine should you want to change it // either to something in this repo or more common feel free to submit a PR . // - your ' user ' isn ' t root // - your user doesn ' t have a PolKit rule allowing access to configure nginx
func TestGetStartTime(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping in short mode")
	}
	testCases := []struct {
		unit      string
		err       error
		opts      Options
		runAsUser bool
	}{
		{"nonexistant", ErrUnitNotActive, Options{UserMode: false}, true},
		{"syncthing", ErrUnitNotActive, Options{UserMode: true}, true},
		{"nginx", nil, Options{UserMode: false}, true},
		{"nonexistant", ErrUnitNotActive, Options{UserMode: false}, false},
		{"nginx", ErrBusFailure, Options{UserMode: true}, false},
		{"nginx", nil, Options{UserMode: false}, false},
	}
	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
	defer cancel()
	Restart(ctx, "syncthing", Options{UserMode: true})
	Stop(ctx, "syncthing", Options{UserMode: true})
	time.Sleep(1 * time.Second)
	for _, tc := range testCases {
		t.Run(fmt.Sprintf("%s as %s, UserMode=%v", tc.unit, userString, tc.opts.UserMode), func(t *testing.T) {
			if (userString == "root" || userString == "system") && tc.runAsUser {
				t.Skip("skipping user test while running as superuser")
			} else if (userString != "root" && userString != "system") && !tc.runAsUser {
				t.Skip("skipping superuser test while running as user")
			}
			ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
			defer cancel()
			_, err := GetStartTime(ctx, tc.unit, tc.opts)
			if err != tc.err {
				t.Errorf("error is %v, but should have been %v", err, tc.err)
			}
		})
	}
	t.Run(fmt.Sprintf("prove start time changes"), func(t *testing.T) {
		if userString != "root" && userString != "system" {
			t.Skip("skipping superuser test while running as user")
		}
		ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
		defer cancel()
		startTime, err := GetStartTime(ctx, "nginx", Options{UserMode: false})
		if err != nil {
			t.Errorf("issue getting start time of nginx: %v", err)
		}
		time.Sleep(1 * time.Second)
		err = Restart(ctx, "nginx", Options{UserMode: false})
		if err != nil {
			t.Errorf("issue restarting nginx as %s: %v", userString, err)
		}
		time.Sleep(100 * time.Millisecond)
		newStartTime, err := GetStartTime(ctx, "nginx", Options{UserMode: false})
		if err != nil {
			t.Errorf("issue getting second start time of nginx: %v", err)
		}
		diff := newStartTime.Sub(startTime).Seconds()
		if diff <= 0 {
			t.Errorf("Expected start diff to be positive, but got: %d", int(diff))
		}
	})
}
func TestGetStartTime ( t * testing . T ) { if testing . Short ( ) { t . Skip ( " " ) } testCases := [ ] struct { unit string err error opts Options runAsUser bool } { { " " , ErrUnitNotActive , Options { UserMode : false } , true } , { " " , ErrUnitNotActive , Options { UserMode : true } , true } , { " " , nil , Options { UserMode : false } , true } , { " " , ErrUnitNotActive , Options { UserMode : false } , false } , { " " , ErrBusFailure , Options { UserMode : true } , false } , { " " , nil , Options { UserMode : false } , false } , } ctx , cancel := context . WithTimeout ( context . Background ( ) , 1 * time . Second ) defer cancel ( ) Restart ( ctx , " " , Options { UserMode : true } ) Stop ( ctx , " " , Options { UserMode : true } ) time . Sleep ( 1 * time . Second ) for _ , tc := range testCases { t . Run ( fmt . Sprintf ( " " , tc . unit , userString , tc . opts . UserMode ) , func ( t * testing . T ) { if ( userString == " " || userString == " " ) && tc . runAsUser { t . Skip ( " " ) } else if ( userString != " " && userString != " " ) && ! tc . runAsUser { t . Skip ( " " ) } ctx , cancel := context . WithTimeout ( context . Background ( ) , 1 * time . Second ) defer cancel ( ) _ , err := GetStartTime ( ctx , tc . unit , tc . opts ) if err != tc . err { t . Errorf ( " " , err , tc . err ) } } ) } t . Run ( fmt . Sprintf ( " " ) , func ( t * testing . T ) { if userString != " " && userString != " " { t . Skip ( " " ) } ctx , cancel := context . WithTimeout ( context . Background ( ) , 5 * time . Second ) defer cancel ( ) startTime , err := GetStartTime ( ctx , " " , Options { UserMode : false } ) if err != nil { t . Errorf ( " " , err ) } time . Sleep ( 1 * time . Second ) err = Restart ( ctx , " " , Options { UserMode : false } ) if err != nil { t . Errorf ( " " , userString , err ) } time . Sleep ( 100 * time . Millisecond ) newStartTime , err := GetStartTime ( ctx , " " , Options { UserMode : false } ) if err != nil { t . Errorf ( " " , err ) } diff := newStartTime . Sub ( startTime ) . Seconds ( ) if diff <= 0 { t . Errorf ( " " , int ( diff ) ) } } ) }
Testing assumptions
 there's no unit installed named `nonexistant`
 the syncthing unit is available but the binary is not on the tester's system
this is just what was available on mine, should you want to change it,
either to something in this repo or more common, feel free to submit a PR.
Testing assumptions there ' s no unit installed named ` nonexistant ` the syncthing unit is available but the binary is not on the tester ' s system this is just what was available on mine should you want to change it either to something in this repo or more common feel free to submit a PR .
// Run these tests only as a user //try nonexistant unit in user mode as user // try existing unit in user mode as user // try existing unit in system mode as user // Run these tests only as a superuser // try nonexistant unit in system mode as system // try existing unit in system mode as system // try existing unit in system mode as system // Prove start time changes after a restart