public void MultipleRestrictionsNothingAllowed() { using (TestHostContext hc = CreateTestContext()) { var restrictions1 = new TaskRestrictions() { SettableVariables = new TaskVariableRestrictions() }; restrictions1.SettableVariables.Allowed.Add("myVar"); restrictions1.SettableVariables.Allowed.Add("otherVar"); _ec.Object.Restrictions.Add(restrictions1); var restrictions2 = new TaskRestrictions() { SettableVariables = new TaskVariableRestrictions() }; _ec.Object.Restrictions.Add(restrictions2); TaskSetVariableCommand setVariable; Command command; var value = "myValue"; // nothing is settable based on the second, empty allowed list int lastCount = _warnings.Count; foreach (String variable in new String[] { "myVar", "otherVar", "neither" }) { command = SetVariableCommand(variable, value); setVariable = new TaskSetVariableCommand(); setVariable.Execute(_ec.Object, command); Assert.Equal(null, _variables.Get(variable)); Assert.Equal(lastCount + 1, _warnings.Count); Assert.Contains("SetVariableNotAllowed", _warnings.Last()); lastCount = _warnings.Count; } } }
public void ExactMatchAllowed() { using (TestHostContext hc = CreateTestContext()) { var restrictions = new TaskRestrictions() { SettableVariables = new TaskVariableRestrictions() }; restrictions.SettableVariables.Allowed.Add("myVar"); restrictions.SettableVariables.Allowed.Add("otherVar"); _ec.Object.Restrictions.Add(restrictions); TaskSetVariableCommand setVariable; Command command; var value = "myValue"; foreach (String variable in new String[] { "myVar", "myvar", "MYVAR", "otherVAR" }) { command = SetVariableCommand(variable, value); setVariable = new TaskSetVariableCommand(); setVariable.Execute(_ec.Object, command); Assert.Equal(value, _variables.Get(variable)); Assert.Equal(0, _warnings.Count); } var badVar = "badVar"; command = SetVariableCommand(badVar, value); setVariable = new TaskSetVariableCommand(); setVariable.Execute(_ec.Object, command); Assert.Equal(null, _variables.Get(badVar)); Assert.Equal(1, _warnings.Count); Assert.Contains("SetVariableNotAllowed", _warnings[0]); } }
public void NoRestrictions() { using (TestHostContext hc = CreateTestContext()) { var variable = "myVar"; var value = "myValue"; var setVariable = new TaskSetVariableCommand(); var command = SetVariableCommand(variable, value); setVariable.Execute(_ec.Object, command); Assert.Equal(value, _variables.Get(variable)); Assert.Equal(0, _warnings.Count); } }
public void MultipleRestrictionsMostRestrictive() { using (TestHostContext hc = CreateTestContext()) { // multiple sets of restrictions, such as from task.json and the pipeline yaml var restrictions1 = new TaskRestrictions() { SettableVariables = new TaskVariableRestrictions() }; restrictions1.SettableVariables.Allowed.Add("my*"); restrictions1.SettableVariables.Allowed.Add("otherVar"); _ec.Object.Restrictions.Add(restrictions1); var restrictions2 = new TaskRestrictions() { SettableVariables = new TaskVariableRestrictions() }; restrictions2.SettableVariables.Allowed.Add("myVar"); restrictions2.SettableVariables.Allowed.Add("myThing"); restrictions2.SettableVariables.Allowed.Add("extra"); _ec.Object.Restrictions.Add(restrictions2); TaskSetVariableCommand setVariable; Command command; var value = "myValue"; // settable is both allowed lists foreach (String variable in new String[] { "myVar", "myThing" }) { command = SetVariableCommand(variable, value); setVariable = new TaskSetVariableCommand(); setVariable.Execute(_ec.Object, command); Assert.Equal(value, _variables.Get(variable)); Assert.Equal(0, _warnings.Count); } // settable in only one int lastCount = _warnings.Count; foreach (String variable in new String[] { "myStuff", "otherVar", "extra", "neither" }) { command = SetVariableCommand(variable, value); setVariable = new TaskSetVariableCommand(); setVariable.Execute(_ec.Object, command); Assert.Equal(null, _variables.Get(variable)); Assert.Equal(lastCount + 1, _warnings.Count); Assert.Contains("SetVariableNotAllowed", _warnings.Last()); lastCount = _warnings.Count; } } }
public void EmptyAllowed() { using (TestHostContext hc = CreateTestContext()) { _ec.Object.Restrictions.Add(new TaskRestrictions() { SettableVariables = new TaskVariableRestrictions() }); var variable = "myVar"; var setVariable = new TaskSetVariableCommand(); var command = SetVariableCommand(variable, "myVal"); setVariable.Execute(_ec.Object, command); Assert.Equal(null, _variables.Get(variable)); Assert.Equal(1, _warnings.Count); Assert.Contains("SetVariableNotAllowed", _warnings[0]); } }
public void EnforcementModeDoesNotImpactPipelineRestrictions() { using (TestHostContext hc = CreateTestContext()) { // This does nothing because this isn't using a TaskDefinitionRestrictions _ec.Setup(x => x.GetVariableValueOrDefault("agent.taskRestrictionsEnforcementMode")).Returns("Disabled"); _ec.Object.Restrictions.Add(new TaskRestrictions() { SettableVariables = new TaskVariableRestrictions() }); var variable = "myVar"; var setVariable = new TaskSetVariableCommand(); var command = SetVariableCommand(variable, "myVal"); setVariable.Execute(_ec.Object, command); Assert.Equal(null, _variables.Get(variable)); Assert.Equal(1, _warnings.Count); Assert.Contains("SetVariableNotAllowed", _warnings[0]); } }
public void TaskDefinitionRestrictionsEnforcementMode(string mode, bool variableExpected, bool warningExpected) { using (TestHostContext hc = CreateTestContext()) { _ec.Setup(x => x.GetVariableValueOrDefault("agent.taskRestrictionsEnforcementMode")).Returns(mode); var definition = new DefinitionData { Name = "TestTask", Version = new DefinitionVersion { Major = 2, Minor = 7, Patch = 1 } }; // allow no variables var restrictions = new TaskDefinitionRestrictions(definition) { SettableVariables = new TaskVariableRestrictions() }; _ec.Object.Restrictions.Add(restrictions); var variable = "myVar"; var value = "myValue"; Command command = SetVariableCommand(variable, value); TaskSetVariableCommand setVariable = new TaskSetVariableCommand(); setVariable.Execute(_ec.Object, command); Assert.Equal((variableExpected ? value : null), _variables.Get(variable)); if (warningExpected) { Assert.Equal(1, _warnings.Count); if (variableExpected) { Assert.EndsWith("SetVariableNotAllowedWarnOnly", _warnings[0]); } else { Assert.EndsWith("SetVariableNotAllowed", _warnings[0]); } } else { Assert.Equal(0, _warnings.Count); } } }