github.com/rancher/elemental/tests@v0.0.0-20240517125144-ae048c615b3f/e2e/helpers/misc/misc.go (about)

     1  /*
     2  Copyright © 2022 - 2024 SUSE LLC
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7      http://www.apache.org/licenses/LICENSE-2.0
     8  Unless required by applicable law or agreed to in writing, software
     9  distributed under the License is distributed on an "AS IS" BASIS,
    10  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  See the License for the specific language governing permissions and
    12  limitations under the License.
    13  */
    14  
    15  package misc
    16  
    17  import (
    18  	"math/rand"
    19  	"time"
    20  )
    21  
    22  /*
    23  Wait for random time
    24    - @param index Modulo for the seed
    25    - @returns Wait for the calculated time
    26  */
    27  func RandomSleep(sequential bool, index int) {
    28  	// Only useful in parallel mode
    29  	if sequential {
    30  		return
    31  	}
    32  
    33  	// Initialize the seed
    34  	r := rand.New(rand.NewSource(time.Now().UnixNano()))
    35  
    36  	// Get a pseudo-random value
    37  	timeMax := 240000
    38  	value := r.Intn(timeMax + (timeMax % index))
    39  
    40  	// Wait until value is reached
    41  	time.Sleep(time.Duration(value) * time.Millisecond)
    42  }
    43  
    44  /*
    45  Wait for nodes to be booted
    46    - @param index Index of the current VM (usually used in a loop)
    47    - @param vmIndex Index of the first booted node
    48    - @param bootedNodes Number of already booted nodes
    49    - @param maxNodes Maximum number of nodes
    50    - @returns Returns (increment) the number of already booted nodes
    51  */
    52  func WaitNodesBoot(index, vmIndex, bootedNodes, maxNodes int) int {
    53  	if (index - vmIndex - bootedNodes) == maxNodes {
    54  		// Wait a little
    55  		time.Sleep(4 * time.Minute)
    56  	}
    57  
    58  	// Save the number of nodes already bootstrapped for the next round
    59  	return (index - vmIndex)
    60  }