Exemple #1
0
        public async Task <IActionResult> postJson([FromBody] InstallationRoot payload, [FromQuery] string repo = "scdfiles")
        {
            try
            {
                // create installation with a 10% chance of failing
                InstallationSim instSim = simHandler.createFailedInstallationByChance(payload, 10);
                StatusType      status  = await instSim.runSetup();

                if (status == StatusType.STATUS_FINISHED_SUCCESS)
                {
                    var jsonString = JsonConvert.SerializeObject(payload, Formatting.Indented);
                    await GitController.createFile("Create: " + payload.installation.name, jsonString, "./installations/" + payload.installation.name + "/" + payload.installation.name + ".json", repo);

                    return(Ok("{\"status\": 200, \"message\": \"Success.\", \"installation_status\": \"" + status + "\"}"));
                }
                else
                {
                    return(BadRequest("{\"status\": 400, \"message\": \"Creation of installation failed.\", \"installation_status\": \"" + StatusType.STATUS_FINISHED_FAILED + "\"}"));
                }
            }
            catch (ApiValidationException)
            {
                return(BadRequest("{\"status\": 400, \"message\": \"File already exists in github repo.\", \"installation_status\": \"" + StatusType.STATUS_FINISHED_FAILED + "\"}"));
            }
            catch (Exception)
            {
                return(BadRequest("{\"status\": 400, \"message\": \"Unknown error.\", \"installation_status\": \"" + StatusType.STATUS_FINISHED_FAILED + "\"}"));
            }
        }
Exemple #2
0
        public async Task createInstallation_installation_fail()
        {
            InstallationSim i1 = simHandler.createFailedInstallation("failed installation 1", 1000, 1000, 1000, output);

            await Task.Run(async() =>
            {
                await i1.runSetup();
            });

            Assert.Equal(StatusType.STATUS_FINISHED_FAILED, i1.status);
        }
Exemple #3
0
        public void create_random_installation_fail()
        {
            InstallationRoot testInst = new InstallationRoot();

            // set data of installation to avoid null reference
            testInst.installation      = new Installation();
            testInst.installation.name = "TEST_INSTALLATION";

            InstallationSim instSim = simHandler.createFailedInstallationByChance(testInst, 100);

            Assert.True(instSim.shouldFail);
        }
Exemple #4
0
        public async Task createInstallation_installations_success(string inst, int startTime, int runTime)
        {
            // sending output as func param to avoid test output bugs compared to making a class field in simHandler
            InstallationSim i1 = simHandler.createSuccessfulInstallation(inst, startTime, runTime, output);

            // use whenall to make it run parallel and speed up mulitple setups
            await Task.WhenAll(
                Task.Run(() => i1.runSetup())
                );

            Assert.Equal(StatusType.STATUS_FINISHED_SUCCESS, i1.status);
        }
Exemple #5
0
        public async Task <IActionResult> stopInstallation([FromBody] StartStopData data)
        {
            StatusType status = await InstallationSim.StopInstallation();

            if (status == StatusType.STATUS_STOPPED)
            {
                return(Ok("{\"status\": 200, \"message\": \"Success.\", \"installation_status\": \"" + status + "\"}"));
            }
            else
            {
                return(BadRequest("{\"status\": 400, \"message\": \"Failed to start installation.\", \"installation_status\": \"" + status + "\"}"));
            }
        }
Exemple #6
0
        public async Task createInstallation_installations_success_and_fail()
        {
            InstallationSim i1 = simHandler.createSuccessfulInstallation("success installation 3", 1000, 1000, output);
            InstallationSim i2 = simHandler.createFailedInstallation("failed installation 2", 1000, 1000, 1000, output);

            // use whenall to make it run parallel and speed up mulitple setups
            await Task.WhenAll(
                Task.Run(() => i1.runSetup()),
                Task.Run(() => i2.runSetup())
                );

            Assert.Equal(StatusType.STATUS_FINISHED_SUCCESS, i1.status);
            Assert.Equal(StatusType.STATUS_FINISHED_FAILED, i2.status);
        }
Exemple #7
0
        public InstallationSim createFailedInstallationByChance(InstallationRoot installation, int chance)
        {
            InstallationSim randomInstallation;

            int installationChance = rand.Next(1, 101);
            int rndMin             = rand.Next(3000, 6001);
            int rndMax             = rand.Next(4000, 10001);

            if (installationChance <= chance)
            {
                randomInstallation = new InstallationSim(installation, rndMin, rndMax, true, 2000);
            }
            else
            {
                randomInstallation = new InstallationSim(installation, rndMin, rndMax, false, 0);
            }

            return(randomInstallation);
        }
Exemple #8
0
        public async Task <IActionResult> copyJson([FromBody] CopyData data, [FromQuery] string repo = "scdfiles")
        {
            try
            {
                IActionResult actionResult = await getJson(data.oldName, repo);

                var content = actionResult as OkObjectResult;

                string jsonString = content.Value.ToString();
                jsonString = jsonString.Replace(data.oldName, data.newName);

                InstallationRoot newInstallation = JsonConvert.DeserializeObject <InstallationRoot>(jsonString);
                // create installation with a 10% chance of failing
                InstallationSim sim = simHandler.createFailedInstallationByChance(newInstallation, 10);
                await sim.runSetup();

                if (sim.status == StatusType.STATUS_FINISHED_SUCCESS)
                {
                    await GitController.CopyFile(data.newName, data.oldName, repo, jsonString);
                }
                else
                {
                    return(BadRequest("{\"status\": 400, \"message\": \"Failed to create file.\", \"installation_status\": \"" + sim.status + "\"}"));
                }
            }
            catch (NullReferenceException)
            {
                return(BadRequest("{\"status\": 400, \"message\": \"Could not find file with the given filename.\"}"));
            }
            catch (ApiValidationException)
            {
                return(BadRequest("{\"status\": 400, \"message\": \"File already exists in github repo.\"}"));
            }
            catch (Exception)
            {
                return(BadRequest("{\"status\": 400, \"message\": \"Unknown error.\"}"));
            }

            return(Ok("{\"status\": 200, \"message\": \"Success.\", \"installation_status\": \"" + StatusType.STATUS_FINISHED_SUCCESS + "\"}"));
        }
Exemple #9
0
        public void create_random_installations()
        {
            int numOfFailedInstallations = 0;

            InstallationRoot testInst = new InstallationRoot();

            // set data of installation to avoid null reference
            testInst.installation      = new Installation();
            testInst.installation.name = "TEST_INSTALLATION";

            for (int i = 0; i < 1000; i++)
            {
                InstallationSim instSim = simHandler.createFailedInstallationByChance(testInst, 50);

                if (instSim.shouldFail)
                {
                    numOfFailedInstallations++;
                }
            }

            // Assume that at least 1 installation will fail on a thousand runs with a 50% chance of failing
            Assert.True(numOfFailedInstallations > 0);
        }
Exemple #10
0
        public InstallationSim createFailedInstallation(string name, int startTime, int runTime, int failTime)
        {
            InstallationSim installationFail = new InstallationSim(name, startTime, runTime, true, failTime);

            return(installationFail);
        }
Exemple #11
0
        public InstallationSim createFailedInstallation(string name, int startTime, int runTime, int failTime, ITestOutputHelper output)
        {
            InstallationSim installationFail = new InstallationSim(name, startTime, runTime, true, failTime, output);

            return(installationFail);
        }
Exemple #12
0
        public InstallationSim createFailedInstallation(InstallationRoot installation, int startTime, int runTime, int failTime)
        {
            InstallationSim installationFail = new InstallationSim(installation, startTime, runTime, true, failTime);

            return(installationFail);
        }
Exemple #13
0
        public InstallationSim createSuccessfulInstallation(string name, int startTime, int runTime)
        {
            InstallationSim installationSuccess = new InstallationSim(name, startTime, runTime, false, 0);

            return(installationSuccess);
        }
Exemple #14
0
        public InstallationSim createSuccessfulInstallation(string name, int startTime, int runTime, ITestOutputHelper output)
        {
            InstallationSim installationSuccess = new InstallationSim(name, startTime, runTime, false, 0, output);

            return(installationSuccess);
        }
Exemple #15
0
        public InstallationSim createSuccessfulInstallation(InstallationRoot installation, int startTime, int runTime)
        {
            InstallationSim installationSuccess = new InstallationSim(installation, startTime, runTime, false, 0);

            return(installationSuccess);
        }